1

我想使用 json 库解析 json 对象。

    {
    "batters":
        {
            "batter":
                [
                    { "id": "1001", "type": "Regular" },
                    { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food" }
                ]
        }

    }
4

2 回答 2

4

使用JSON ..

JSONObject object = new JSONObject(yourString);
JSONObject batters = object.getJSONObject("batters");
JSONArray batter = batters.getJSONArray("batter");
for(int i = 0 ; i < batter.length() ; i++) { 
JSONObject object1 = (JSONObject) batter.get(i);
    String id = object1.getString("id");
}
于 2012-07-17T12:43:43.040 回答
1
{
"result": "success",
"countryCodeList":
[
  {"countryCode":"00","countryName":"World Wide"},
  {"countryCode":"kr","countryName":"Korea"}
] 
}

下面我正在获取国家/地区详细信息

JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);

JSONArray valArray1 = valArray.getJSONArray(1);

valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");

int len = valArray1.length();

for (int i = 0; i < valArray1.length(); i++) {

 Country country = new Country();
 JSONObject arr = valArray1.getJSONObject(i);
 country.setCountryCode(arr.getString("countryCode"));                        
 country.setCountryName(arr.getString("countryName"));
 arrCountries.add(country);
}
于 2012-07-18T06:09:08.840 回答