Java 在Android中解析JSON数组和对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28736419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:42:41  来源:igfitidea点击:

Parsing JSON array and object in Android

javaandroidarraysjson

提问by Adreamcathcer Adreamcatcher

This is what the JSON looks like:

这是 JSON 的样子:

[{
    "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:

我正在尝试在 Android 中使用以下 Java 代码解析它:

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

JSONObject jObj = null; 尝试 { jObj = 新 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.

我没有得到任何结果。我怎样才能成功解析这个 JSON?我正在使用 Android Studio。

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

另外,如果数组中有多个部分,我们如何确保打印出每个部分?

采纳答案by Niranj Patel

Your JSON string start with JSONArray.

您的 JSON 字符串以 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

查看Android JSON解析教程

回答by ρяσ?ρ?я K

As in posted json String jsonStris JSONArray of JSONObeject's instead of JOSNObject of JSONArray.

在发布的 json 字符串中,jsonStrJSONObeject 的 JSONArray 而不是 JSONArray 的 JOSNObject

So convert jsonStrString to JSONArray:

所以将jsonStr字符串转换为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);

回答by Midhun Mundayadan

in this exammple details object contain j son data

在此示例中,详细信息对象包含 j 儿子数据

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

回答by Ravi Thapliyal

Your root object is a JSON array [], not a JSON object {}there. So, you need

您的根对象是一个 JSON 数组[],而不是{}那里的 JSON 对象。所以,你需要

jObj = new JSONArray(jsonStr);
jObj = jObj.getJSONObject(0);

The rest of your code will now work fine treating jObjas a JSONObject. The concept here is exactly the same as what you're doing for your propertiesJSON array.

您的其余代码现在可以正常工作,将其jObj视为JSONObject. 这里的概念与您为propertiesJSON 数组所做的完全相同。

回答by irtheitroadoys

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

回答by Android Team

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

}