简体   繁体   English

JSONArray解析错误

[英]JSONArray Parsing error

I need help with parsing a JSON array. 我需要解析JSON数组的帮助。

[
    {
        "Header1": [
            {
                "path": "upload/images/1430572021716.jpg"
            },
            {
                "path": "upload/images/1430574003703.jpg"
            }
        ]
    },
    {
        "Header2": [
            {
                "path": "upload/images/1430574124119.jpg"
            },
            {
                "path": "upload/images/1430574203001.jpg"
            }
        ]
    }
]

I am receiving the above JSONArray perfectly. 我完美地收到了上面的JSONArray。 I want to iterate through the array and extract both the header text "Header1" and the value of path 我想遍历数组并提取标题文本“ Header1”和path的值

I keep running into the following error message 我不断遇到以下错误消息

at 0 of type org.json.jsonarray cannot be converted to jsonobject

after some research, this is due to the system not being able to parse to a JSON array. 经过研究后,这是由于系统无法解析为JSON数组。 It does work if I change the "list" array to an objct, however this is not an array and i loose the ability to iterate through it. 如果我将“列表”数组更改为objct,它确实可以工作,但是,这不是数组,因此我失去了遍历它的能力。

Here is the code i tried to parse the array with 这是我试图解析数组的代码

    JSONArray mArray;

    mArray = json;

    //download json array
    for (int i = 0; i < mArray.length(); i++) {
        if(mArray != null) {
            JSONArray list = mArray.getJSONArray(i);
            if(list != null) {
                for(int a = 0; a < list.length();a++) {
                    JSONObject elem = list.getJSONObject(a);
                    if (elem != null) {
                            listdata.add(elem.getString("path"));

                    }
                }
            }
        }
    }

You're trying to treat each element of the top-level array as another array - it's not, it's an object. 您正在尝试将顶级数组的每个元素都视为另一个数组-不是,它是一个对象。 That object then has another field whose value is an array. 该对象然后有另一字段,其值一个数组。 So you want something like: 所以你想要像这样的东西:

for (int i = 0; i < json.length(); i++) {
    JSONObject container = json.getJSONObject(i);
    // We don't know the property name, but there's only one, apparently...
    String key = container.keys().next();
    JSONArray subarray  = container.getJSONArray(key);
    for (int j = 0; j < subarray.length(); j++) {
        listdata.add(subarray.getJSONObject(j).getString("path"));
    }
}

The second level of your JSON consists in your JSON array elements. JSON的第二级包含JSON数组元素。

What you need, as you already mention, is to get them as JSONObject . 正如您已经提到的,您需要将它们作为JSONObject获得。

Then you getJSONArray from that object: 然后从该对象getJSONArray

//download json array
for (int i = 0; i < mArray.length(); i++) {
    if(mArray != null) {
        JSONObject element = mArray.getJSONObject(i);
        JSONArray list = element.getJSONArray("Header1");
        if(list != null) {

However, since the "Header..." keys are changing, you might need to infer the key by invoking keys() . 但是,由于"Header..."键正在更改,因此您可能需要通过调用keys()来推断keys()

一个更简单的方法是使用GSON库并创建类似于JSON的POJO类,这是一个不错的网站

This works for JavaScript and very simple. 这适用于JavaScript,非常简单。

//mArray is your json array
let newArray = JSON.stringify(mArray);
mArray = JSON.parse(newArray);

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

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