Gson将长度为1的json数组解析为JsonArray对象而不是JsonObject对象[duplicate]

时间:2021-02-12 13:42:40

This question already has an answer here:

这个问题在这里已有答案:

Using Gson, I am trying to force my json objects to have an array value with one element. Although I construct the json this way, the JsonParser creates a new JsonObject in place of my array of length 1. The majority of json in my system have value with arrays greater than length 1 and parse correctly.

使用Gson,我试图强制我的json对象具有一个元素的数组值。虽然我以这种方式构造json,但JsonParser创建了一个新的JsonObject来代替我的长度为1的数组。我系统中的大多数json都具有值大于长度为1的数组并正确解析。

I discovered this while attempting a utility method for brute forcing a JsonObject to JsonArray. It's not working out as I would expect.

我在尝试使用实用程序方法强制JsonObject到JsonArray时发现了这一点。它没有像我期望的那样发挥作用。

static JsonArray bruteForceJsonArray(JsonObject object, String key) {
    if(object.get(key).isJsonArray()) {
        return object.get(key).getAsJsonArray();
    } else {
        System.out.println("broken: " + object.toString());
        JsonObject temp = object.get(key).getAsJsonObject();
        String fixed = "{'" + key +  "':[" + temp.toString() + "]}";
        System.out.println("pre-fix: " + fixed);

        //parsing here reduces the array to an object - hot to prevent?     
        temp = new JsonParser().parse(fixed).getAsJsonObject();         
        System.out.println("fixed: " + object.toString());
        return object.get(key).getAsJsonArray();
    }
}

output:

broken: {"token":{"NER":"O","id":1.0,"word":".","CharacterOffsetEnd":765.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":764.0}}
pre-fix: {'token':[{"NER":"O","id":1.0,"word":".","CharacterOffsetEnd":765.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":764.0}]}
fixed: {"token":{"NER":"O","id":1.0,"word":".","CharacterOffsetEnd":765.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":764.0}}

An example of the majority json as they appear correctly in the system:

多数json的一个例子,因为它们在系统中正确显示:

{"token":[{"Speaker":"PER0","POS":"IN","lemma":"except","CharacterOffsetBegin":742.0,"NER":"O","id":1.0,"word":"Except","CharacterOffsetEnd":748.0},{"CharacterOffsetEnd":752.0,"Speaker":"PER0","POS":"IN","lemma":"for","CharacterOffsetBegin":749.0,"NER":"O","id":2.0,"word":"for"},{"id":3.0,"word":"extra","CharacterOffsetEnd":758.0,"Speaker":"PER0","POS":"JJ","lemma":"extra","CharacterOffsetBegin":753.0,"NER":"O"},{"Speaker":"PER0","POS":"NN","lemma":"meat","CharacterOffsetBegin":759.0,"NER":"O","id":4.0,"word":"meat","CharacterOffsetEnd":763.0},{"CharacterOffsetEnd":764.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":763.0,"NER":"O","id":5.0,"word":"."}]}

Is there a clever way out of this problem - some way I can effectively force my objects into arrays so I can process them in stream without too much try/catch or if/else handling?

有没有一个聪明的方法摆脱这个问题 - 某种方式我可以有效地强制我的对象进入数组,所以我可以在流中处理它们没有太多的try / catch或if / else处理?

1 个解决方案

#1


static JsonArray bruteForceJsonArray(JsonObject object, String key) {
    if (object.get(key).isJsonArray()) {
        return object.get(key).getAsJsonArray();
    } else {
        JsonArray oneElementArray = new JsonArray();
        oneElementArray.add(new JsonObject());
        return oneElementArray;
    }
}

Although, you shouldn't work with JsonObject/JsonArray directly. Make a POJO class for your data and use Gson to deserialize JSON.

虽然,您不应该直接使用JsonObject / JsonArray。为您的数据创建一个POJO类,并使用Gson反序列化JSON。

#1


static JsonArray bruteForceJsonArray(JsonObject object, String key) {
    if (object.get(key).isJsonArray()) {
        return object.get(key).getAsJsonArray();
    } else {
        JsonArray oneElementArray = new JsonArray();
        oneElementArray.add(new JsonObject());
        return oneElementArray;
    }
}

Although, you shouldn't work with JsonObject/JsonArray directly. Make a POJO class for your data and use Gson to deserialize JSON.

虽然,您不应该直接使用JsonObject / JsonArray。为您的数据创建一个POJO类,并使用Gson反序列化JSON。