“预期为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING"

2022-01-30 00:00:00 json parsing java gson

我有这个方法:

public static Object parseStringToObject(String json) {
    String Object = json;
    Gson gson = new Gson();
    Object objects = gson.fromJson(object, Object.class);
    parseConfigFromObjectToString(object);
    return objects;
}

我想用以下方式解析 JSON:

And I want to parse a JSON with:

public static void addObject(String IP, Object addObject) {
    try {
        String json = sendPostRequest("http://" + IP + ":3000/config/add_Object", ConfigJSONParser.parseConfigFromObjectToString(addObject));
        addObject = ConfigJSONParser.parseStringToObject(json);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

但我收到一条错误消息:

But I get an error message:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

推荐答案

即使没有看到您的 JSON 字符串,您也可以从错误消息中看出它不是正确的结构来解析为您的类的实例.

Even without seeing your JSON string you can tell from the error message that it is not the correct structure to be parsed into an instance of your class.

Gson 期望您的 JSON 字符串以对象左大括号开头.例如

Gson is expecting your JSON string to begin with an object opening brace. e.g.

{

但是你传递给它的字符串以一个开引号开始

But the string you have passed to it starts with an open quotes

"

相关文章