Reputation: 29679
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("-----------------");
Upvotes: 7
Views: 27213
Reputation: 280102
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
Upvotes: 13
Reputation: 1164
I know it's old. This is my solution if you need to convert a JSON into a list and you don't have direct setters in your object.
Let's say that you have this JSON structure of 'Players':
JSON:
{
"Players":
[
{
"uid": 1, "name": "Mike",
"stats": {"shots" : 10, "hits": 5}
},
{
"uid": 2, "name": "John",
"stats": {"shots": 4, "hits": 1}
}
]
}
getListOfPlayersFromJson:
public static List<Player> getListOfPlayersFromJson(String json) {
List<Player> players = new ArrayList<>();
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
root.at("/Players").forEach(node -> {
Player p = getPlayerFromNode(node);
players.add(p);
});
} catch (IOException e) {
e.printStackTrace();
}
return players;
}
getPlayerFromNode:
public static Player getPlayerFromNode(JsonNode node) {
Player player = new Player();
player.setUid(node.at("/uid").longValue());
player.setName(node.at("/name").asText());
player.setStats(
node.at("/stats/shots").asInt(),
node.at("/stats/hits").asInt()
);
return player;
}
Upvotes: 0
Reputation: 29679
Based on the answer that Sotirios Delimanolis gave me, here was my solution:
ObjectMapper mapper = new ObjectMapper();
JsonFactory jfactory = mapper.getFactory();
JsonParser jParser;
try {
jParser = jfactory.createParser( tFile );
JsonNode node = mapper.readTree( jParser);
int count = node.get("count").asInt();
for ( int i = 0; i < count; i++ ) {
System.out.print( "City: " + node.get("list").get(i).get("name").asText() );
System.out.println( " , Absolute temperature: " +
node.get("list").get(i).get("main").get("temp").asText() );
}
jParser.close();
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 3