一尘不染

如何从Android中的URL解析多个JSON对象和数组?我在这个例子中使用我想要的例子,

json

我想在服务器端进行以下编辑

我按照这个例子

但在那边,他正在通过网址使用一个json对象调用数据

叫做“世界人口”

但我有一个Json数据,其中包含很多Json对象和数组,..如下代码所示

{ "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"]
     },

像这样,我有很多数据,在上面的示例中我想要这些数据。不改变任何东西在服务器端..或Restful服务,..谢谢


阅读 256

收藏
2020-07-27

共1个答案

一尘不染

响应

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"); 
        }
    }
    }
    }
2020-07-27