如何解析嵌套JSON结果的动态JSON关键 [英] How to parse a dynamic JSON key in a Nested JSON result

查看:189
本文介绍了如何解析嵌套JSON结果的动态JSON关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON结果在下面的格式, JSON林特显示这是一个有效响应。

I have a JSON result in the following format which JSON Lint shows this as a "Valid Response".

我的问题是:我怎么访问question_mark,因为141的内容,8911等都是动态值?

My question is: how do I access the content of "question_mark" since "141", "8911", etc are all dynamic values?

我的样品code来访问产品的价值。

My sample code for accessing value of "product".

//Consider I have the first <code>JSONObject</code> of the "search_result" array and 
//I access it's "product" value as below.
String product = jsonObject.optString("product"); //where jsonObject is of type JSONObject.
//<code>product<code> now contains "abc".

JSON:

{
 "status": "OK",
 "search_result": [

            {
                "product": "abc",
                "id": "1132",
                "question_mark": {
                    "141": {
                        "count": "141",
                        "more_description": "this is abc",
                        "seq": "2"
                    },
                    "8911": {
                        "count": "8911",
                        "more_desc": "this is cup",
                        "seq": "1"
                    }
                },
                "name": "some name",
                "description": "This is some product"
            },
            {
                "product": "XYZ",
                "id": "1129",
                "question_mark": {
                    "379": {
                        "count": "379",
                        "more_desc": "this is xyz",
                        "seq": "5"
                    },
                    "845": {
                        "count": "845",
                        "more_desc": "this is table",
                        "seq": "6"
                    },
                    "12383": {
                        "count": "12383",
                        "more_desc": "Jumbo",
                        "seq": "4"
                    },
                    "257258": {
                        "count": "257258",
                        "more_desc": "large",
                        "seq": "1"
                    }
                },
                "name": "some other name",
                "description": "this is some other product"
            }
       ]
}

我的提问标题为动态密钥可能是错误的,但我不知道在这一点上有什么针对此问题的更好的名字做。

My question title "dynamic key" could be wrong but I don't know at this point what's a better name for this issue.

任何帮助将是很大的AP preciated!

Any help would be greatly appreciated!

推荐答案

使用 JSONObject的键()拿到钥匙,然后遍历每个键进入动态值。

Use JSONObject keys() to get the key and then iterate each key to get to the dynamic value.

大约在code将是这样的:

Roughly the code will look like:


    // searchResult refers to the current element in the array "search_result"
    JSONObject questionMark = searchResult.getJSONObject("question_mark");
    Iterator keys = questionMark.keys();

    while(keys.hasNext()) {
        // loop to get the dynamic key
        String currentDynamicKey = (String)keys.next();

        // get the value of the dynamic key
        JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);

        // do something here with the value...
    }

这篇关于如何解析嵌套JSON结果的动态JSON关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆