繁体   English   中英

Android Json解析复杂数组

[英]Android Json parsing with complicated arrays

[
  {
    "countries": [
      {
        "id": 1,
        "country": "India"
      },
      {
        "id": 2,
        "country": "Australia"
      },
      {
        "id": 3,
        "country": "Srilanka"
      },
      {
        "id": 4,
        "country": "Pakistan"
      },
      {
        "id": 5,
        "country": "Switzerland"
      }
    ]
  }
]

如何在Android中解析此Json Response。 我没有任何适当的解决方案。 请帮我。

像这样做...

JSONArray arr = locs.getJSONArray("countries");


for (int i = 0; i < arr.length(); ++i) {
    JSONObject rec = arr.getJSONObject(i);
    int id = rec.getInt("id");
    String con = rec.getString("country");
    // ...
}
   JSONArray jArr=new JSONArray(response);
    for(int i=0;i<jArr.length;i++)
    {
    id[]=jArr.getJSONObject(i).getInt("id");
    con[]=jArr.getJSONObject(i).getString("country");
    }

http://developer.android.com/reference/org/json/JSONTokener.html

我也看到了杰克逊在Android应用程序中使用过。

 JSONArray countriesobj = new JSONArray();   
 JSONObject json;
            try {
                JSONObject jObject = new JSONObject(response);
                json = jObject;

                countriesobj = json.getJSONArray("countries");

                country = new String[countriesobj.length()];
                for (int i = 0; i < countriesobj.length(); i++) {

                    JSONObject e = countriesobj.getJSONObject(i);

                    country[i] = e.getString("country");

                }

            } catch (Exception e) {
                e.printStackTrace();

            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM