微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解析另一个JSONArray中的JSONArray

如何解决如何解析另一个JSONArray中的JSONArray

我正在尝试解析JSONObject。 此JSONObject中包含一个JSONArray,并且它在JSONArray中具有另一个JSONArray。 我要解析的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","extraorders":[
                {
                    "extra_id":"2",{
                    "extra_id":"19","extra_price":"500","extra_count":"1"
                }
            ]
        },{
            "menu_id":"6","menu_defaultprice":"2000","extraorders":[
                {
                    "extra_id":"6",{
                    "extra_id":"21",{
                    "extra_id":"41","extra_count":"1"
                }
            ]
        }
    ]
}

下面的代码是我以前尝试过的。

@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
        }
    }
}

显示错误是..

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

此外,我正在使用此json模块依赖项。

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

我知道,如果我使用System.out.println(OBJECT)在控制台上打印某些内容,则对象的toString() 方法调用。因此,我尝试调用toString(),这给了我ClassCastException异常。

解决方法

错误实际上在ArrayList<JSONArray> jsonArrayList = (ArrayList<JSONArray>)jsonObject.get("orders");

您可以遍历JSONArray并读取其属性,而不是将ArrayList强制转换为JSONArray。您的代码可以更改为类似的内容。

    @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"));
            }
        }
    }
,

您错误地获取了orders。它应该存储在JSONArray中,而不是ArrayList

替换

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

使用

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

现在,您可以像这样迭代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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。