Question

I have the following data and I am asked to de-serialize it. But how to de-serialize the following json data?

"drug": "a:1:{s:6:\"drug_0\";a:3:{s:11:\"drug_name_0\";s:4:\"Napa\";s:13:\"drug_dosage_0\";d:1;s:15:\"drug_duration_0\";d:1;}}"
Was it helpful?

Solution

Your JSON is incorrect. This should be the correct JSON

String json = "{\"drug\": \"a:1:{s:6:\\\"drug_0\\\";a:3:{s:11:\\\"drug_name_0\\\";s:4:\\\"Napa\\\";s:13:\\\"drug_dosage_0\\\";d:1;s:15:\\\"drug_duration_0\\\";d:1;}}\"}";
JSONObject jsonObject = new JSONObject(json);
String drug = jsonObject.getString("drug");

Using this you can get value of drug as a:1:{s:6:\"drug_0\";a:3:{s:11:\"drug_name_0\";s:4:\"Napa\";s:13:\"drug_dosage_0\";d:1;s:15:\"drug_duration_0\";d:1;}}

OTHER TIPS

You'll have to do something like this:

JSONObject json = new JSONObject("your json string");

and from there get primitives or nested json jbjects by calling methods such as: getInt(), getString(), getJSONObject().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top