Answer the question
In order to leave comments, you need to log in
How to bind JSON to POJO without parent element?
There is this JSON:
{
"data":[
{
"store_id":"6217704612",
"created":"1587466128",
"price":"5000",
"amount":"1"
},
{
"store_id":"6217704613",
"created":"1587466130",
"price":"2000",
"amount":"2"
}
]
}
@Getter
@Setter
public class MyJSON {
@JsonProperty("data")
private List<MyData> data;
}
@Entity
@Getter
@Setter
public class MyData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@JsonProperty("store_id")
private long storeId;
private Timestamp created;
private double price;
private double amount;
}
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
MyJSON myJSON = mapper.readValue(jsonString, MyJSON.class);
List<MyData> myData = myJSON.getData();
Answer the question
In order to leave comments, you need to log in
Try like this:
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString).path("data");
List<MyData> myJSON = mapper.readValue(node.toString(), new TypeReference<List<MyData>>() {});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question