我在字符串
中有这样的json数据 {
'MMTStaticCountrySearchResponse':{
'Country':[
{
'-CountryCode':'IN',
'CitiesCount':'362',
'CountryName':'India',
'HotelsCount':'6535'
},
{
'-CountryCode':'SG',
'CitiesCount':'1',
'CountryName':'Singapore',
'HotelsCount':'197'
}
]
}
}
如何访问和存储国家/地区名称?
答案 0 :(得分:1)
杰克逊:
final ObjectMapper mapper = new ObjectMapper()
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
final JsonNode node = mapper.readTree(...);
for (final JsonNode element: node.get("MMTStaticCountrySearchResponse").get("Country")) {
node.get("CountryName");
// etc
}
答案 1 :(得分:0)
JSONObject obj = new JSONObject("JsonString");
Object temp = obj.get("MMTStaticCountrySearchResponse");
List<String> list = new ArrayList<String>();
JSONObject objtemp = new JSONObject(temp.toString());
JSONArray array = objtemp.getJSONArray("Country");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("HotelsCount"));
System.out.println(array.getJSONObject(i).getString("CountryName"));
System.out.println(array.getJSONObject(i).getString("HotelsCount"));
}