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

quot;Expected BEGIN_OBJECT but was STRING at line 1 column 1quot;(“预期为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING)

本文介绍了“预期为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个方法:

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

"

这篇关于“预期为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:“预期为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING"

基础教程推荐