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

前端之家收集整理的这篇文章主要介绍了android-如果名称不断变化,如何检索JSON对象的值 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在MediaWiki API的帮助下获取有关某些动物的一些信息
http://en.wikipedia.org/w/api.PHP?action=query\u0026amp;prop=extracts\u0026amp;titles=Animal\u0026amp;format=json\u0026amp;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."
         }
      }
   }
}
最佳答案
您应该考虑以下几点:

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

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?

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

原文链接:https://www.f2er.com/android/531367.html

猜你在找的Android相关文章