简体   繁体   English

如何解析另一个 JSONArray 中的 JSONArray

[英]How to parse JSONArray which is in another JSONArray

I am trying to parse a JSONObject.我正在尝试解析 JSONObject。 This JSONObject has a JSONArray in it, and it has another JSONArray inside of JSONArray.这个 JSONObject 中有一个 JSONArray,它在 JSONArray 中还有另一个 JSONArray。 The json form that I am trying to parse is as below.我试图解析的 json 形式如下。

{
    "phone":"01029093199",
    "store_id":"1",
    "orders":[
        {
            "menu_id":"4",
            "menu_defaultprice":"1500",
            "extraorders":[
                {
                    "extra_id":"1",
                    "extra_price":"0",
                    "extra_count":"1"
                },
                {
                    "extra_id":"38",
                    "extra_price":"300",
                    "extra_count":"2"
                }
            ]
        },
        {
            "menu_id":"4",
            "menu_defaultprice":"1500",
            "extraorders":[
                {
                    "extra_id":"2",
                    "extra_price":"0",
                    "extra_count":"1"
                },
                {
                    "extra_id":"19",
                    "extra_price":"500",
                    "extra_count":"1"
                }
            ]
        },
        {
            "menu_id":"6",
            "menu_defaultprice":"2000",
            "extraorders":[
                {
                    "extra_id":"6",
                    "extra_price":"0",
                    "extra_count":"1"
                },
                {
                    "extra_id":"21",
                    "extra_price":"500",
                    "extra_count":"1"
                },
                {
                    "extra_id":"41",
                    "extra_price":"300",
                    "extra_count":"1"
                }
            ]
        }
    ]
}

The code below is what I have tried before.下面的代码是我之前尝试过的。

@RestController
public class OrderApiController {

    private OrderService orderService;

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping("/OrderInsert.do")
    public void insertOrder(@RequestBody JSONObject jsonObject) {
        JSONParser jsonParser = new JSONParser();

        System.out.println(jsonObject);
        System.out.println(jsonObject.get("phone")); // phone 가져오기 성공
        System.out.println(jsonObject.get("store_id"));  // store_id 가져오기 성공
        System.out.println("==========JSONArray Parsing start=========");
        ArrayList<JSONArray> jsonArrayList = (ArrayList<JSONArray>)jsonObject.get("orders");
        for(int i = 0; i < jsonArrayList.size(); i++) {
            System.out.println(jsonArrayList.get(i));  // SUCCESS

            String temp = jsonArrayList.get(i).toJSONString(); // WHERE ERROR HAPPENS
            System.out.println(temp);

            // Tried below code to remove "[", "]" from JSONArray, but not working.
            // Error message was same as the message shown from line 37.
            //String jsonString = temp.substring(1, temp.length()-1);
            //System.out.println(jsonString);
            

            // org.json.JSONObject jTemp = new org.json.JSONObject(jsonArrayList.get(i));
            // System.out.println(jTemp);  --> prints {} (empty JSONObject)
            // System.out.println("menu_id : " + jTemp.getInt("menu_id")); // Not Working
        }
    }
}

The error shown is..显示的错误是..

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.json.simple.JSONArray

Additionally, I am using this json module dependency.此外,我正在使用这个 json 模块依赖项。

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20200518</version>
</dependency>

I knew that if I print something on console using System.out.println(OBJECT) , the OBJECT's toString() method is called.我知道如果我使用System.out.println(OBJECT)在控制台上打印一些东西,就会调用 OBJECT 的toString()方法。 So I tried to call toString() , and that gave me the ClassCastException exception.所以我尝试调用toString() ,这给了我 ClassCastException 异常。

Error is in fact at ArrayList<JSONArray> jsonArrayList = (ArrayList<JSONArray>)jsonObject.get("orders");错误实际上是在ArrayList<JSONArray> jsonArrayList = (ArrayList<JSONArray>)jsonObject.get("orders");

Instead of casting JSONArray to ArrayList , you can traverse JSONArray and read its attributes.您可以遍历JSONArray并读取其属性,而不是将JSONArray转换为ArrayList Your code can be changed something like.您的代码可以更改为。

    @PostMapping("/OrderInsert.do")
    public void insertOrder(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("phone")); // phone 가져오기 성공
        System.out.println(jsonObject.get("store_id"));  // store_id 가져오기 성공
        System.out.println("==========JSONArray Parsing start=========");
        JSONArray orders = jsonObject.getJSONArray("orders");
        for(int i = 0; i < orders.length(); i++) {
            System.out.println(orders.get(i));  // SUCCESS

            String temp = orders.get(i).toString();
            System.out.println(temp);

            // Travserse further json
            JSONObject order = orders.getJSONObject(i);
            System.out.println(order.get("menu_defaultprice"));
            System.out.println(order.get("menu_id"));
            JSONArray extraorders = order.getJSONArray("extraorders");
            for(int j = 0; j < extraorders.length(); j++) {
                JSONObject extraOrder = extraorders.getJSONObject(j);
                System.out.println(extraOrder.get("extra_id"));
                System.out.println(extraOrder.get("extra_price"));
                System.out.println(extraOrder.get("extra_count"));
            }
        }
    }

You are incorrectly fetching the orders .您错误地获取orders It should be stored in JSONArray, and NOT ArrayList它应该存储在 JSONArray 中,而不是 ArrayList

Replace :替换

ArrayList<JSONArray> jsonArrayList = (ArrayList<JSONArray>)jsonObject.get("orders");

With :

JSONArray jsonArrayList = jsonObject.getJSONArray("orders");

Now, you can iterate JSONArray like this现在,您可以像这样迭代 JSONArray

for(int i = 0; i < jsonArrayList.length(); i++) {
   System.out.println(jsonArrayList.get(i));  // SUCCESS

   String temp = jsonArrayList.get(i).toJSONString();
   System.out.println(temp);
}

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

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