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

我正在MediaWiki API的帮助下获取有关某些动物的一些信息
http://en.wikipedia.org/w/api.php?action=queryu0026amp;prop=extractsu0026amp;titles=Animalu0026amp;format=jsonu0026amp;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,camelsu2014which are uniquely suited 
             to their desert habitatsu2014are a vital means of transport for 
             passengers and cargo. There are three surviving species of 
             camel."
         }
      }
   }
}
最佳答案
您应该考虑以下几点:

>仅在响应时查找响应状态代码,例如:

onResponse(){
如果(statusCode == 200){
   // —现在在这里查找特定于响应的主体
 }
}
>使用JsonObject作为响应主体的模型,例如:

@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
  }
}

您如何获得此回复?翻新,凌空,OkHttp?

每个都提供了一些易于解决的方法来更改字段

以上是来客网为你收集整理的android-如果名称不断变化,如何检索JSON对象的值 全部内容,希望文章能够帮你解决android-如果名称不断变化,如何检索JSON对象的值 所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。