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

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

问题描述

我有这个方法:

pre $ public static Object parseStringToObject(String json){
String Object = json;
Gson gson = new Gson();
Object objects = gson.fromJson(object,Object.class);
parseConfigFromObjectToString(object);
返回对象;
}

我想用以下语句解析JSON:

  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();
}
}

但我收到错误讯息:


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



解决方案

即使没有看到您的JSON字符串,您可以从错误消息中知道它不是正确的结构被解析成你的类的一个实例。



Gson希望你的JSON字符串以一个对象大括号开头。例如

  {

但是您传递给它的字符串以开引号开头

 


I have this method:

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

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: Expected BEGIN_OBJECT but was STRING at line 1 column 1

解决方案

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 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天全站免登陆