How To Parse A JSON Without Key In Android?
Hi i am having a simple type of json response ,I have worked on JSON with keys but in this response i only have the values,Please see the response as below json { status: 'success'
Solution 1:
Your Json is not correct please check below format for your requirement.
{
status: "success",
country: [
{
"name": "Afghanistan",
"value": 1
},
{
"name": "Albania",
"value": 2
},
{
"name": "Algeria",
"value": 3
},
{
"name": "American Samoa",
"value": 4
},
{
"name": "Andorra",
"value": 5
},
{
"name": "Angola",
"value": "6"
}
....
....
....
]
}
Solution 2:
The "status","country" and all the "1","2", etc in the json array "country" are all keys and hence, must be in double quotes. In short, your JSON is invalid. Example:
{
"status": "success",
"country": [
{
"1": "Afghanistan"
}
]}
Solution 3:
I have done it my way and wasted time in changing JSON syntax as answers suggested,my code is as below.
code
String jsonStr = sh.makeServiceCall(allContriesURL,
BackendAPIService.GET);
try {
if (jsonStr != null) {
jsonObj = new JSONObject(jsonStr);
if (jsonObj.has("country")) {
CountryArray = jsonObj.getJSONArray("country");
if (CountryArray != null && CountryArray.length() != 0) {
// looping through All Contacts
System.out
.println("::::::::::::::::my talent size:::::::::::"
+ CountryArray.length());
for (int i = 0; i < CountryArray.length(); i++) {
JSONObject c = CountryArray.getJSONObject(i);
country_name = c.getString((i + 1) + "");
System.out
.println(":::::::::::::::::::COUNTRY NAME:::::::::::::::::::::"
+ country_name);
HashMap<String, String> countryMap = new HashMap<String, String>();
countryMap.put("country_id", i + 1 + "");
countryMap.put("name", country_name);
countryList.add(countryMap);
}
}
}
}
} catch (Exception e) {
System.out
.println("::::::::::::::::::::::;;EXCEPTION::::::::::::::::"
+ e);
}
Post a Comment for "How To Parse A JSON Without Key In Android?"