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

如何在 Android 中解析这个 Json 响应。我没有得到任何适当的解决方案。请帮我。

4

4 回答 4

4

像这样做...

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");
    // ...
}
于 2012-08-13T04:16:21.433 回答
1
   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");
    }
于 2012-08-13T04:17:26.593 回答
0

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

我还看到Jackson在 Android 应用程序中使用。

于 2012-08-13T04:15:01.257 回答
0
 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();

            }
于 2012-08-13T04:57:39.000 回答