我对解析特定json
字符串有疑问。我没有找到任何对我的情况有帮助的东西。我有这个 json :
{
"AM": {
"country_name": "Armenia",
"data": {
"180854": {
"time_published": "2012-03-30 13:31:39",
"title": "Example",
},
"180926": {
"time_published": "2012-04-02 05:21:44",
"title": "Example",
}, // keeps going on
}
},
"BM": {
"country_name": "Bolivia",
"data": {
"180854": {
"time_published": "2012-03-30 13:31:39",
"title": "Example",
},
"180926": {
"time_published": "2012-04-02 05:21:44",
"title": "Industrial PPI inflation eases to 5.5% y/y in February",
}
}
}, // continues
}
所以我想知道的是有没有办法获得它的价值JSONObject
:AM
,BM
。它们每次都不同,所以我无法检查它们,但我希望能够将它们保存为String
. 这是我实际用来解析 JSON 的内容:
public static void jsonParser(String jsonBuffer){
try {
//String jsonData = new String(jsonBuffer,"UTF-8");
JSONObject json = (JSONObject) new JSONTokener(jsonBuffer).nextValue();
//JSONObject country = json.getJSONObject(String.valueOf(json.keys().next()));
Log.d("","json : "+json);
Iterator<Object> keys = json.keys();
while (keys.hasNext()) {
JSONObject country = json.getJSONObject(String.valueOf(keys.next()));
String countryName = country.getString("country_name");
Log.e("","country_name: "+countryName);
JSONObject data = country.getJSONObject("data");
Iterator<Object> keys2 = data.keys();
while(keys2.hasNext()){
JSONObject datas = data.getJSONObject(String.valueOf(keys2.next()));
String published = datas.getString("time_published");
Log.d("","published : "+published);
String title = datas.getString("title");
Log.w("","title : "+title);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
那么有什么建议我怎样才能得到这些值?