android - 如何检查JSON中的值

标签 android json

我有这个 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/

相关文章:

android - 如何获取word中的特殊字符及其点击事件

android颜色检测opencv

ios - 进行 JSON 序列化时出错 ErrorDomain Code=3840

javascript - jQuery .each 循环不循环,但当你 console.log 时却循环?

c# - 动态处理 JSON 变量

java - 将 JSONArray 转换为 arrayList

android - 是什么导致 FragmentPagerAdapter 创建的 fragment 在适配器调用 onDestroy 后仍然为 FragmentManager 所知?

android - 覆盖通知声音Android

java - 2D ArrayList 索引越界

javascript - 如何将更多对象附加到 JSON 对象而不覆盖它?