英文:
Parse json to java object: Expected BEGIN_OBJECT but was BEGIN_ARRAY
问题
以下是翻译好的内容:
有人能帮我解析 JSON 字符串为对象吗?我在尝试,但总是得到这样的错误:
预期是 BEGIN_OBJECT,但在第1行第3列处却是 BEGIN_ARRAY,路径为 $[0]
以下是我的代码:
Item 类:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Item {
@JsonProperty("id")
String id;
@JsonProperty("dTime")
long dTime;
@JsonProperty("aTime")
long aTime;
}
将 JSON 转换为对象:
String jsonString = "[
[
{
\"id\":\"string\",
\"dTime\": 1111111111,
\"aTime\": 1111111111
},
{
\"id\":\"string\",
\"dTime\": 1111111111,
\"aTime\": 1111111111
}
]
]";
Gson gson = new Gson();
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(jsonString, listType);
System.out.println(gson.toJson(list));
**更新:**
因为我从外部 API 得到这个 JSON 作为响应:
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
是否可能在这个 restTemplate.exchange(..) 内部将其转换为 Item 对象的数组?还是使用 gson 的方式更好?
英文:
Can someone help me with parsing json string to object, I´m trying but always get error like this:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
Here is my code:
Item class:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Item{
@JsonProperty("id")
String id;
@JsonProperty("dTime")
long dTime;
@JsonProperty("aTime")
long aTime;
}
converting json to object:
String jsonString = "[
[
{
"id":"string",
"dTime": 1111111111,
"aTime": 1111111111
},
{
"id":"string",
"dTime": 1111111111,
"aTime": 1111111111
}
]
]";
Gson gson = new Gson();
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(jsonString, listType);
System.out.println(gson.toJson(list));
Update:
Because i get this json as response from external API:
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
Is it possible to make this converting to Array of Item objects inside this restTemplate.exchange(..) or this with gson is better way?
答案1
得分: 1
您可以使用 ParameterizedTypeReference
来直接映射 Java 列表对象,并且由于您的数据包装在 []
中,可以使用 List<List<Item>>
。
ParameterizedTypeReference<List<List<Item>>> responseType =
new ParameterizedTypeReference<List<List<Item>>>() {};
ResponseEntity<List<List<Item>>> response =
restTemplate.exchange(url, HttpMethod.POST, request, responseType);
英文:
You can use ParameterizedTypeReference
to directly map in java list object and use List<List<Item>>
since your data wrap in []
ParameterizedTypeReference<List<List<Item>>> responseType =
new ParameterizedTypeReference<List<List<Item>>>() {};
ResponseEntity<List<List<Item>>> response =
restTemplate.exchange(url, HttpMethod.POST, request, responseType);
答案2
得分: 0
你的jsonString多了一个'['。应该是
String jsonString = "
[
{
"id":"string",
"dTime": 1111111111,
"aTime": 1111111111
},
{
"id":"string",
"dTime": 1111111111,
"aTime": 1111111111
}
]
";
英文:
Your jsonString has an extra '['. It should be
String jsonString = "
[
{
"id":"string",
"dTime": 1111111111,
"aTime": 1111111111
},
{
"id":"string",
"dTime": 1111111111,
"aTime": 1111111111
}
]
";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论