I;m trying to parse a json string which I receive from web server as a response, but I have a strange issue. In my json I have a few JSONObjects, which I never know what will be their names and I should cycle them depending on how much keys it has. But the problem is with the code I’m using it’s getting the same value everytime even when I know that there is other values too.
Here is the cod which I’m using :
JSONObject json = (JSONObject) new JSONTokener(jsonBuffer).nextValue();
JSONObject country = json.getJSONObject(String.valueOf(json.keys().next()));
Iterator<Object> keys = json.keys();
while (keys.hasNext()) {
String countryName = country.getString("country_name");
Log.e("","country_name: "+countryName);
String data = country.getString("data");
Log.e("","data : "+data);
}
and here is what my json looks like :
"AZ": {
"country_name": "Azerbaijan",
"data": {
"181261": {
"time_published": "2012-04-04 15:55:29",
"title": "Azerbaijan, Turkey not to change stakes in TANAP",
"body": null
},
"181260": {
"time_published": "2012-04-04 15:53:10",
"title": "SOCAR mulls acquisition of Swiss refinery",
},
"181061": {
"time_published": "2012-04-03 05:53:00",
"title": "Azerbaijan, Lithuania mull LNG terminal investment",
},
// and so on....
}
}, // and it keeps going on
Any ideas how my jsonParser should looks like?
I’m not very familiar with Android and Java, but I assume it should be:
From my comments:
It looks to me that you have an endless
whileloop, it actually never gets out to assign a new value tocountry.You have to iterate over all keys, get the country object for the key, and then access it’s values.
Currently you are getting the first country element, get the keys and just check whether the iterator contains keys. But you are not advancing the iterator.