【问题标题】:Parsing JSON array and object in Android在 Android 中解析 JSON 数组和对象
【发布时间】:2015-04-28 11:22:25
【问题描述】:

这就是 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"
        }
    ]
}]

我正在尝试在 Android 中使用以下 Java 代码对其进行解析:

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

虽然我没有得到任何结果。如何成功解析这个 JSON?我正在使用 Android Studio。

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

【问题讨论】:

    标签: java android arrays json


    【解决方案1】:

    您的 JSON 字符串以 JSONArray 开头。

    这里有示例代码,试试看。

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

    查看Android JSON Parsing Tutorial

    【讨论】:

    • 这会打印出一个结果,谢谢。我想知道,如果不是太多......我如何打印所有条目,例如如果有多个 prop_id?
    • 这是for循环,因此您可以将所有数据添加到列表中
    • 好的,如果 JSON 是 [{"pmid":"2","name":"MANAGEMENT","re​​sult":"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","re​​sult":"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":"俱乐部公寓","address":"4600 Barrington Club","city":"Columbus ","state":"OH","zip":"43520","lat":"40.436","long":"-83.048"}]}]
    【解决方案2】:

    正如发布的 json String jsonStrJSONObeject 的 JSONArray 而不是 JSONArray 的 JOSNObject

    所以将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);
    

    【讨论】:

      【解决方案3】:

      这里是完整的分辨率示例。

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

      【讨论】:

        【解决方案4】:

        在此示例中,详细信息对象包含 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);
                            }
        

        【讨论】:

          【解决方案5】:

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

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

          您的其余代码现在可以将jObj 视为JSONObject。这里的概念与您为 properties JSON 数组所做的完全相同。

          【讨论】:

            【解决方案6】:

            使用这个

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

            【讨论】:

              猜你喜欢
              • 2016-11-20
              • 1970-01-01
              • 1970-01-01
              • 2015-06-10
              • 1970-01-01
              • 2016-08-09
              • 1970-01-01
              • 1970-01-01
              • 2011-08-04
              相关资源
              最近更新 更多