android - 如果名称不断变化,如何检索 JSON 对象的值

标签 android json

我正在借助 mediawiki api 检索有关某些动物的一些信息 http://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Animal&format=json&exintro=1 但其中一个对象是根据动物而变化的页面。如何访问页面对象(它包含有关动物的摘录)?

这是JSON查询结果

    {  
   "batchcomplete":"",
   "warnings":{  },
   "query":{  
      "pages":{  
         "6598":{  
            "pageid":6598,
            "ns":0,
            "title":"Camel",
            "extract":"<p class=\"mw-empty-elt\">\n</p>\n<p>A <b>camel</b> is 
             an even-toed ungulate in the genus <i>Camelus</i> that bears 
             distinctive fatty deposits known as \"humps\" on its back. 
             Camels have long been domesticated and, as livestock, they 
             provide food (milk and meat) and textiles (fiber and felt from 
             hair). As working animals, camels\u2014which are uniquely suited 
             to their desert habitats\u2014are a vital means of transport for 
             passengers and cargo. There are three surviving species of 
             camel."
         }
      }
   }
}

最佳答案

有几件事你应该考虑:

  1. 只需在响应时查找响应状态代码,例如:

    onResponse(){ 如果(状态码 == 200){ //--- 现在在这里寻找特定的响应主体 }

  2. 使用 JsonObject 作为 Response Body 的模型,例如:

@Override
public void onResponse(Call < JsonObject > call, Response < JsonObject > response) {
  if (response.code() == 200) {
    if (!response.body().toString().isEmpty()) {
     // ----- Now Here You can Extract the JsonObject Field you are looking for
     // --- Example :
     
     JsonObject object = response.body();
     String target = object.get("field_name);
     
    } else {
      // Empty Response
    }
  } else {
    // Failure or whatever
  }
}

您如何获得此响应? Retrofit、Volley、OkHttp??

每个都为更改字段提供了某种简单的解决方法

关于android - 如果名称不断变化,如何检索 JSON 对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55433821/

相关文章:

java - 使用java从apache solr获取json对象进行自动完成

java - 使用 Jackson 库从 JSON 文件创建 JAVA Map

javascript - 系统.数据.实体.动态代理

ios - 如何使用segue快速将json数组发送到不同的 View Controller

java - 联系Android API,没有任何显示

android - SubscriptionInfo.getMnc() 为具有不同运营商的两个 SIM 返回相同的值

java - 读取已拆分为两个文件的 Java 序列化对象?

Android Emulator (AVD) 在虚拟机 (VMWARE Workstation) 中运行的 Ubuntu 中运行速度太慢

javascript - 两个具有通用工厂和 ng-repeat 刷新功能的 Controller

android - 更改sdk版本时,遇到Theme.Dialog错误