quot;Expected BEGIN_OBJECT but was STRING at line 1 column 1quot;(“预期为 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"
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
