简体   繁体   English

在JAVA中遍历多个JSON arrays

[英]Traversing Multiple JSON arrays in JAVA

I was wondering if anyone could help.我想知道是否有人可以提供帮助。 Is there an easy and efficient way to traverse a JSON object if we are given the position of an error in that JSON object? Is there an easy and efficient way to traverse a JSON object if we are given the position of an error in that JSON object? For example, say I hit a service with the JSON request below and it returns a bad request stating that the value in the field: Cars[1].例如,假设我使用下面的 JSON 请求访问了一个服务,它返回一个错误的请求,说明字段中的值: Cars[1]。 Manufacturers [2].CarDetails[Doors] is invalid (Last element of request – Toyota Hybrid with 100 doors).制造商 [2].CarDetails[Doors]无效(请求的最后一个元素 - 带有 100 个门的 Toyota Hybrid)。
Is there an easy way to traverse the request to pull the registration for that car given the error that is returned?鉴于返回的错误,是否有一种简单的方法可以遍历请求以提取该汽车的注册? ie in the example below the value for doors on the Toyato Hybrid is invalid due to it being 100. I want to be able to traverse my JSON request to pull back the registration of the vehicle with the issue.即在下面的示例中,Toyato Hybrid 上的门值无效,因为它是 100。我希望能够遍历我的 JSON 请求以撤回有问题的车辆的注册。 In this example “Reg123” should be returned.在此示例中,应返回“Reg123”。

Any help is greatly appreciated.任何帮助是极大的赞赏。

"Cars": [{
    "Manufacturers": [{
            "Manufacturer": "Audi"
            "CarDetails": {
                "Model": "A4",
                "Doors": "3",
                "Registration": "abc123",
                "Transmission": "Automatic"
            }
        }, {
            "Manufacturer": "BMW",
            "CarDetails": {
                "Model": "X1",
                "Doors": "3",
                "Registration": "abc123",
                "Transmission": "Manual"
            }
        }, {
            "Manufacturer": "Renault"
            "CarDetails": {
                "Model": "Megane",
                "Doors": "3",
                "Registration": "abc123",
                "Transmission": "Automatic"
            }
        }
    ]
}, {
    "Manufacturers": [{
            "Manufacturer": "Citroen"
            "CarDetails": {
                "Model": "C3",
                "Doors": "3",
                "Registration": "abc123",
                "Transmission": "Automatic"
            },
        }, {
            "Manufacturer": "Seat"
            "CarDetails": {
                "Model": "Leon",
                "Doors": "3",
                "Registration": "abc123",
                "Transmission": "Automatic"
            },
        }, {
            "Manufacturer": "Toyota",
            "CarDetails": {
                "Model": "Hybrid",
                "Doors": "100",
                "Registration": "Reg123",
                "Transmission": "Manual"
            }
        }
    ]
}

] ]

If you are using spring boot, you can use如果您使用 spring 引导,则可以使用

org.springframework.boot.configurationprocessor.json

package and do something like this: package 并执行以下操作:

response.getJSONArray("Cars").getJSONObject(1).getJSONArray("Manufacters").getJSONObject(2).getJSONObject("CarDetails").getString("Doors")

In addition to this line you must handle the JSONException exception除了这一行,您还必须处理 JSONException 异常

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

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