我正在借助 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."
}
}
}
}
最佳答案
有几件事你应该考虑:
只需在响应时查找响应状态代码,例如:
onResponse(){ 如果(状态码 == 200){ //--- 现在在这里寻找特定的响应主体 }
使用 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/