1

This is what the JSON looks like:

[{
    "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"
        }
    ]
}]

I am trying to parse it with the following Java code in Android:

JSONObject jObj = null; try { jObj = new JSONObject(jsonStr);

    // We get weather info (This is an array)
    JSONArray jArr = jObj.getJSONArray("properties");

    // We use only the first value
    //JSONObject JSONWeather = jArr.getJSONObject(0);
    JSONObject c = jArr.getJSONObject(0);
    String name = c.getString(TAG_NAME);
    String email = c.getString(TAG_EMAIL);
    String phone = c.getString(TAG_PHONE);
} catch (JSONException e) {
    e.printStackTrace();
}

return null;

I am not getting any results though. How can I successfully parse this JSON? I'm using Android Studio.

Also, if there were multiple pieces to the array, how could we make sure each one of them is printed out?

6 Answers 6

14

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

Sign up to request clarification or add additional context in comments.

3 Comments

This prints out one result, thank you for that. I am wondering, if it is not too much... how can I print all entries out, if there are multiple prop_id for example?
this is for loop so you can add all data in list
Okay, what if the JSON was [{"pmid":"2","name":"MANAGEMENT","result":"1","properties":[{"prop_id":"32","prop_name":" Tower","address":"281 Lakeshore","city":"Euclid","state":"OH","zip":"44142","lat":"54.5","long":"-81.5034"}]},{"pmid":"1","name":"ONE","result":"18","properties":[{"prop_id":"3","prop_name":"Chase","address":"146 Goon Blvd.","city":"City","state":"OH","zip":"12345","lat":"46.35","long":"-83.1138"},{"prop_id":"6","prop_name":"Club Apartments","address":"4600 Barrington Club","city":"Columbus","state":"OH","zip":"43520","lat":"40.436","long":"-83.048"}]}]
2

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);

Comments

2

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();
    }
}

}

Comments

0

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);
                    }

Comments

0

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.

Comments

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();
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.