This is what the JSON looks like:
[{
\"pmid\": \"2\",
\"name\": \" MANAGEMENT\",
\"result\": \"1\",
\"properties\": [
{
\
Your root object is a JSON array []
, not a JSON object {}
there. So, you need
jObj = new JSONArray(jsonStr);
jObj = jObj.getJSONObject(0);
The rest of your code will now work fine treating jObj
as a JSONObject
. The concept here is exactly the same as what you're doing for your properties
JSON array.
Here is complete example with resolution.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args)
{
JSONObject jObj = null;
try {
String jsonStr = "[{\"pmid\":\"2\",\"name\":\" MANAGEMENT\",\"result\":\"1\",\"properties\":[{\"prop_id\":\"32\",\"prop_name\":\"Bonneville\",\"address\":\"122 Lakeshore\",\"city\":\"Ripley\",\"state\":\"OH\",\"zip\":\"11454\",\"lat\":\"41.123\",\"long\":\"-85.5034\"}]}]";
jsonStr = jsonStr.substring(1, jsonStr.length()-1);
System.out.println(jsonStr);
jObj = new JSONObject(jsonStr);
System.out.println("pmid="+jObj.get("pmid"));
System.out.println("name="+jObj.get("name"));
System.out.println("result="+jObj.get("result"));
JSONArray jArr = jObj.getJSONArray("properties");
JSONObject c = jArr.getJSONObject(0);
System.out.println("prop_id=="+c.get("prop_id"));
System.out.println("prop_name=="+c.get("prop_name"));
System.out.println("address=="+c.get("address"));
System.out.println("city=="+c.get("city"));
System.out.println("state=="+c.get("state"));
System.out.println("zip=="+c.get("zip"));
System.out.println("lat=="+c.get("lat"));
System.out.println("long=="+c.get("long"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
}
Your JSON string start with JSONArray.
Here sample code, try it.
JSONArray mJsonArray = new JSONArray(jsonStr);
JSONObject mJsonObject = mJsonArray.getJSONObject(0);
String pmid = mJsonObject.getString("pmid");
String name = mJsonObject.getString("name");
String result = mJsonObject.getString("result");
JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
for (int i = 0; i < mJsonArrayProperty.length(); i++) {
JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(i);
String prop_id = mJsonObjectProperty.getString("prop_id");
String prop_name = mJsonObjectProperty.getString("prop_name");
String address = mJsonObjectProperty.getString("address");
String city = mJsonObjectProperty.getString("city");
String state = mJsonObjectProperty.getString("state");
String zip = mJsonObjectProperty.getString("zip");
String lat = mJsonObjectProperty.getString("lat");
String lon = mJsonObjectProperty.getString("long");
}
Check Android JSON Parsing Tutorial
in this exammple details object contain j son data
JSONObject details = mJSONParser.doInBackground(); //json object
Child_Registration_StaticData deta=new Child_Registration_StaticData();
try
{
deta.UniqueID = details.getString("UniqueID");
deta.Nameofchild= details.getString("Nameofchild");
deta.FatherName= details.getString("FatherName");
deta.DOB= details.getString("DOB");
child_name.setText(deta.Nameofchild);
father_name.setText(deta.FatherName);
dateof_birth.setText(deta.FatherName);
}
As in posted json String jsonStr
is JSONArray of JSONObeject's instead of JOSNObject of JSONArray.
So convert jsonStr
String to JSONArray
:
JSONArray jArray = new JSONArray(jsonStr);
JSONObject c = jArray.getJSONObject(0);
// get properties JSONArray from c
JSONArray jArrProperties = c.getJSONArray("properties");
JSONObject jsonObject = jArrProperties.getJSONObject(0);
use this
try {
JSONArray array0 = new JSONArray(Sample);
JSONObject object0 = array0.getJSONObject(0);
JSONArray array1 = object0.getJSONArray("properties");
JSONObject object1 = array1.getJSONObject(0);
String name = object1.getString("prop_name");
} catch (JSONException e) {
e.printStackTrace();
}