How to parse Multiple JSON Objects and arrays from a url in android? i am using a example i want it in that example,

StackOverflow https://stackoverflow.com/questions/20513370

  •  31-08-2022
  •  | 
  •  

Domanda

I want below process on with out editing on server side

I followed this example

but over there he is calling data with one json object from a url

called "worldpopulation",..

but i have a Json data which is having lot of Json objects and arrays,.. like below code

{ "getting data":"ok"
   "Todays":population
   {"results"{[
   "worldpopulation": 
[
     {
     "rank":1,"country":"China",
     "population":"1,354,040,000",
     "flag":["http://www.androidbegin.com/tutorial/flag/china.png"]
     }, 
{
    "population":{
     [
    "countrypulation"        {
     "rank":2,"country":"India",
     "population":"1,210,193,422",
     "flag":["http://www.androidbegin.com/tutorial/flag/india.png"]
     }, 

like that i have a lot of data i want this data in that above example. Without changing any thing on server side,.. or Restful service,.. thank you

È stato utile?

Soluzione

response

JSONObject JObject = new JSONObject(response);
      String getting_data = JObject.getstring("getting data");
    String Todays= JObject.getstring("Todays");
    JsonArray results =JObject.getjsonarray("results");
    for(int i = 0 ; i < results.length(); i++){
     JsonObject resultsobject =results.getjsonobject(i);

    JsonArray worldpopulation 
=resultsobject.getjsonarray("worldpopulation");
         for(int j = 0 ; j < worldpopulation.length(); j++){
     JsonObject worldpopulationobject =worldpopulation.getjsonobject(j);
     JsonObject emptyobject =worldpopulationobject.getjsonobject("");
         String rank=emptyobject .getstring("rank"); 
        String population=emptyobject .getstring("population"); 
        JsonArray flagarray =countrypulation.getjsonarray(flag);
        for(int n = 0 ; n < flagarray.length(); n++){
        JsonObject flagarrayobject =flag1array.getjsonobject(n);
        String flag=flag1arrayobject .getstring("flag"); 
        }

     JsonObject populationobject 
=worldpopulationobject.getjsonobject("population");
         JsonArray emptyarray =JObject.getjsonarray("");
    for(int k = 0 ; k < emptyarray.length(); k++){
        JsonObject emptyarrayobject =emptyarray.getjsonobject(k);
        JsonObject 
countrypulation=emptyarrayobject.getjsonobject("countrypulation");

         String rank1=countrypulation .getstring("rank"); 
        String country=countrypulation .getstring("country"); 
        String population1=countrypulation .getstring("population");
        JsonArray flag1array =countrypulation.getjsonarray(flag);
        for(int m = 0 ; m < flag1array.length(); m++){
        JsonObject flag1arrayobject =flag1array.getjsonobject(m);
        String flag1=flag1arrayobject .getstring("flag"); 
        }
    }
    }
    }

Altri suggerimenti

You parse

{
    ...
}

as JSONObject and

[
    ...
]

as JSONArray. JSONArray contains your objects. From the JSONObject you can retrieve the property values.

JSONArray response = new JSONArray('your json string');

for (int i = 0; i < response.length(); i++) {
    response.getJSONObject(i).getString("property"));
}

BTW your json is invalid, event if you close the missing brackets. Check it here http://jsonlint.com/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top