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

查看:38
本文介绍了如何解析嵌套 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?

我用于访问产品"值的示例代码.

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" but whats searchResult?
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天全站免登陆