“预期为 BEGIN_OBJECT,但在第 1 行第 1 列处为 STRING" [英] "Expected BEGIN_OBJECT but was STRING at line 1 column 1"

查看:48
本文介绍了“预期为 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆