繁体   English   中英

如何从android中的url解析多个JSON对象和数组? 我正在使用一个例子,我想在那个例子中,

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

我希望下面的过程在服务器端进行编辑

我按照这个例子

但在那里他用一个url中的一个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服务,..谢谢

响应

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

你解析

{
    ...
}

作为JSONObject和

[
    ...
]

作为JSONArray。 JSONArray包含您的对象。 从JSONObject中,您可以检索属性值。

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

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

顺便说一句,如果你关闭了缺少的括号,你的json是无效的。 在这里查看http://jsonlint.com/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM