How do I iterate over a JSON response in Java using Jackson API? In other words, if the response has a list and inside that list is another list ( in this case called 'weather') , then how do I get the temperature?
Here is an example of what I am trying to iterate through:
{
"message":"like",
"cod":"200",
"count":3,
"list":[
{
"id":2950159,
"name":"Berlin",
"coord":{
"lon":13.41053,
"lat":52.524368
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"temp":74
}
]
},
{
"id":2855598,
"name":"Berlin Pankow",
"coord":{
"lon":13.40186,
"lat":52.56926
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"temp":64
}
]
}
]
}
And here is the code I am trying to use, which doesn't work, because I can only iterate through the first item:
try {
JsonFactory jfactory = new JsonFactory();
JsonParser jParser = jfactory.createJsonParser( new File("test.json") );
// loop until token equal to "}"
while ( jParser.nextToken() != JsonToken.END_OBJECT ) {
String fieldname = jParser.getCurrentName();
if ( "list".equals( fieldname ) ) { // current token is a list starting with "[", move next
jParser.nextToken();
while ( jParser.nextToken() != JsonToken.END_ARRAY ) {
String subfieldname = jParser.getCurrentName();
System.out.println("- " + subfieldname + " -");
if ( "name".equals( subfieldname ) ) {
jParser.nextToken();
System.out.println( "City: " + jParser.getText() ); }
}
}
}
jParser.close();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("-----------------");
You are parsing the JSON when Jackson is meant to do it for you. Don't do this to yourself.
One option is to create a DTO (Data Transfer Object) that matches the format of your JSON
class Root {
private String message;
private String cod;
private int count;
private List<City> list;
// appropriately named getters and setters
}
class City {
private long id;
private String name;
private Coordinates coord;
private List<Weather> weather;
// appropriately named getters and setters
}
class Coordinates {
private double lon;
private double lat;
// appropriately named getters and setters
}
class Weather {
private int id;
private String main;
private String description;
private int temp;
// appropriately named getters and setters
}
Then use an ObjectMapper
and deserialize the root of the JSON.
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(yourFileInputStream, Root.class);
You can then get the field you want. For example
System.out.println(root.getList().get(0).getWeather().get(0).getTemp());
prints
74
The alternative is to read your JSON in as a JsonNode
and traverse it until you get the JSON element you want. For example
JsonNode node = mapper.readTree(new File("text.json"));
System.out.println(node.get("list").get(0).get("weather").get(0).get("temp").asText());
also prints
74
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With