i'm trying to consume a Json response using RestTemplate in java with Jackson annotations, and i have a resource that have many name properties like so:
{
-name1:{
id:2,
price:12,
name:"Bob:
},
-name2:{
id:111,
price:1.1,
name:"Ron:
},
-name3:{
id:10,
price:33,
name:"jhon:
},
}
and the list go on like this. this is my code of how to get one of the entities, like name1 object:
public class Class1 {
private RestTemplate restTemplate;
private String url = "https://url.com/api";
private Response response;
private Market market ;
public class1(){
restTemplate = new RestTemplate();
response = restTemplate.getForObject(url,Response.class);
}
@Override
public Market getResults() {
market = response.getResult();
System.out.println(Market);
return null;
}
}
and the response class is like so :
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
public class Response {
@JsonProperty("name1")
private Market result;
}
how can i get all those elements as an array or ArrayList? this API is from 3rd party website and there's many entities liek that in the Json response. thanks in advance.
So in the Json above, it is not an array but a list of key value pair.
This is what an array looks like in Json:
Then what you could have done is:
But since your example is a map, you need to to use a MAP
That post actually similar to yours: Reading JSON map structure via spring boot