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

标签 android json restful-url

我希望在不在服务器端进行编辑的情况下进行以下处理

我关注了this例子

但在那边,他正在使用来自 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"); 
        }
    }
    }
    }

关于android - 如何从 android 中的 url 解析多个 JSON 对象和数组?我在那个例子中使用了一个我想要的例子,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20513370/

相关文章:

java - putExtras(child class) - 接收基类

android - 项目的构建脚本中的gradle版本号

java - 想获取php数据到android

json - 雅典娜 : use only a subset of JSON fields

iis - Request和Response在服务栈中如何得到处理?

android - 绘制多边形并填充它

android - 如何在 android 布局的边框上设置我的 circleImageView?

mysql - JSON 打印时带有反斜杠

java - PUT 和 POST 获取 Restful Web 服务的 405 方法不允许错误

api - REST API 子资源