如何在嵌套的JSON结果中解析动态JSON密钥? [英] How to parse a dynamic JSON key in a Nested JSON result?

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

问题描述

我有以下格式的JSON结果, JSON Lint 将此显示为有效响应。

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?

我的示例代码,用于访问product的值。

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.

非常感谢任何帮助!

推荐答案

使用 JSONObject keys()获取密钥然后迭代每个键获得动态值。

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

大致代码如下:


    // 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天全站免登陆