我對解析特定的json
字符串有疑問。在我的情況下,我找不到任何有助於我的東西。我有這樣的JSON:Android獲取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();
}
}
因此,如何獲得這些值?
你在當前代碼有什麼問題? – MKJParekh 2012-04-14 13:46:23