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?
List<List<Item>>or strip the extra pair of[and]:jsonString.substring(1, jsonString.length() - 1)List<List<Item>>.List<List<Item>>immediately, without extra manual parsing withGson