java Gson:不是 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30717179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:33:33  来源:igfitidea点击:

Gson: Not a JSON Object

javajsongson

提问by marknorkin

I have the following String passed to server:

我将以下字符串传递给服务器:

{
    "productId": "",
    "sellPrice": "",
    "buyPrice": "",
    "quantity": "",
    "bodies": [
        {
            "productId": "1",
            "sellPrice": "5",
            "buyPrice": "2",
            "quantity": "5"
        },
        {
            "productId": "2",
            "sellPrice": "3",
            "buyPrice": "1",
            "quantity": "1"
        }
    ]
}

which is a valid json for http://jsonlint.com/

这是http://jsonlint.com/的有效 json

I want to get the bodies array field.

我想获取 body 数组字段。

That's how I'm doing it:

这就是我的做法:

Gson gson = new Gson();
JsonObject object = gson.toJsonTree(value).getAsJsonObject();
JsonArray jsonBodies = object.get("bodies").getAsJsonArray();

But on the second line I'm getting exception listed below:

但在第二行,我得到下面列出的异常:

HTTP Status 500 - Not a JSON Object: "{\"productId\":\"\",\"sellPrice\":\"\",\"buyPrice\":\"\",\"quantity\":\"\",\"bodies\":[{\"productId\":\"1\",\"sellPrice\":\"5\",\"buyPrice\":\"2\",\"quantity\":\"5\"},{\"productId\":\"2\",\"sellPrice\":\"3\",\"buyPrice\":\"1\",\"quantity\":\"1\"}]}"

How to do it properly then ?

那该怎么做呢?

回答by Sotirios Delimanolis

Gson#toJsonTreejavadocstates

Gson#toJsonTreejavadoc状态

This method serializes the specified object into its equivalent representation as a tree of JsonElements.

此方法将指定的对象序列化为JsonElements树的等效表示形式。

That is, it basically does

也就是说,它基本上是

String jsonRepresentation = gson.toJson(someString);
JsonElement object = gson.fromJson(jsonRepresentation, JsonElement.class);

A Java Stringis converted to a JSON String, ie. a JsonPrimitive, not a JsonObject. In other words, toJsonTreeis interpreting the content of the Stringvalue you passed as a JSON string, not a JSON object.

JavaString转换为 JSON 字符串,即。一个JsonPrimitive,不是一个JsonObject。换句话说,toJsonTree是将String您传递的值的内容解释为 JSON 字符串,而不是 JSON 对象。

You should use

你应该使用

JsonObject object = gson.fromJson(value, JsonObject.class);

directly, to convert your Stringto a JsonObject.

直接将您的转换StringJsonObject.

回答by bbill

I have used the parsemethod as described in https://stackoverflow.com/a/15116323/2044733before and it's worked.

我之前使用过https://stackoverflow.com/a/15116323/2044733 中parse描述的方法并且它有效。

The actual code would look like

实际代码看起来像

JsonParser jsonParser = new JsonParser();
jsonParser.parse(json).getAsJsonObject();

From the docsit looks like you're running into the error described where your it thinks your toJsonTreeobject is not the correct type.

文档看来,您遇到了所描述的错误,它认为您的toJsonTree对象类型不正确。

The above code is equivalent to

上面的代码等价于

JsonObject jelem = gson.fromJson(json, JsonElement.class);

as mentioned in another answer here and on the linked thread.

如此处和链接线程上的另一个答案所述。

回答by JoseQ

What about JsonArray jsonBodies = object.getAsJsonArray("bodies");

怎么样JsonArray jsonBodies = object.getAsJsonArray("bodies");