简体   繁体   English

如何使用嵌套数组在 Java 中解析 JSON 字符串

[英]How to do Parsing JSON string in Java with nested arrays

Starting from the similar question, Nested JSON Array in Java I have a somewhat odd json as a response from a REST api(POST) call.从类似的问题开始,Java 中的嵌套 JSON 数组我有一个有点奇怪的 json 作为来自 REST api(POST) 调用的响应。 I need to find out the id and name of the sub_items array in each items array element.我需要在每个items数组元素中sub_items数组的idname

I tried like given below for which I am getting error like我试过下面给出的我收到错误

org.json.JSONException: JSONObject["items.sub_items"] not found. org.json.JSONException: 未找到 JSONObject["items.sub_items"]。

I also tried just 'sub_items' as the parameter also, but no.我也尝试过将 'sub_items' 作为参数,但没有。 I am using GSON.我正在使用 GSON。 No choice use others.没有选择使用别人。

final JSONObject jsonObj = new JSONObject(JSON_STRING);
final JSONArray subItems = jsonObj.getJSONArray("items.sub_items");
final int       n       = subItems.length();
for (int i = 0; i < n; ++i) {
    final JSONObject subI= subItems.getJSONObject(i);
    System.out.println("id="+subI.getString("id"));
    System.out.println("name="+subI.getString("name"));
}               

The following is my json as a response from a REST api call.以下是我的 json 作为来自 REST api 调用的响应。

{
  "menu": {
    "items": [{
            "id": 1,
            "code": "hot1_sub1_mnu",
            "name": "Mutton",
            "status": "1",
            "sub_items": [{
                "id": "01",
                "name": "Mutton Pepper Fry",
                "price": "100"
            }, {
                "id": "02",
                "name": "Mutton Curry",
                "price": "100"
            }]
        },
        {
            "id": "2",
            "code": "hot1_sub2_mnu",
            "name": "Sea Food",
            "status": "1",
            "sub_items": [{
                "id": "01",
                "name": "Fish Fry",
                "price": "150"
            }]
        },
        {
            "id": "3",
            "code": "hot1_sub3_mnu",
            "name": "Noodles",
            "status": "1",
            "sub_items": [{
                "id": "01",
                "name": "Chicken Noodles",
                "price": "70"
            }, {
                "id": "02",
                "name": "Egg Noodles",
                "price": "60"
            }]
        }
    ]
}}

As I said, my JSON is a response from an REST api call, and it is 7300 lines long, like shown below with the outline, as shown in code beautifier.正如我所说,我的 JSON 是来自 REST api 调用的响应,它有 7300 行长,如下图所示,并带有轮廓,如代码美化器所示。

object      {3}
    infoTransferReqResp     {1}
paramExtens :   null
    pageGroups      {1}
    pageGroups      [1]
    0       {4}
page_group_name :   LFG - LG - Insured Summary
page_group_id   :   8100
    **pages     [3]**
    repeatingBlocks     {1}

I needed to extract a string value 'questionText' from inside 'Pages' array.我需要从“Pages”数组中提取字符串值“questionText”。 I used jsonPath() method with navigation path as given below which solved the problem.我使用 jsonPath() 方法和导航路径,如下所示,解决了这个问题。

JsonPath                 jsonPathEvaluator = response.jsonPath();
List<List<List<String>>> questions         = jsonPathEvaluator.getList("pageGroups.pageGroups.pages.questions.questionText");

It gave me 3 ArrayLists one embedded in another corresponding to 3 arrays of ' pages '它给了我 3 个 ArrayLists,一个嵌入另一个对应于 3 个“页面”数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM