Parsing JSON array and object in Android

2020-03-08 06:20发布

问题:

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?

回答1:

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



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

}


回答3:

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


回答4:

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


回答5:

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.



回答6:

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