我有这个 JSON 我想检查这个 JSON 有 "song"
或 "fun"
然后获取值和键 "price"
:
{
"has_error": false,
"response": {
"song": {
"price": "2000"
},
"jok": {
"price": "free_for_now"
}
},
"state_code": 200
}
最佳答案
JSONObject 类有一个有方法。
引用链接:http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
如果此对象具有名称映射,则返回 true。映射可能为 NULL。
例如
if (json.has("song")) {
JSONObject song = jsonObject.getJSONObject("song");
}
if (json.has("fun")) {
JSONObject fun = jsonObject.getJSONObject("fun");
}
关于android - 如何检查JSON中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45126609/