How to do a JSON response repetition in Java using the Jackson API? In other words, if there is a list in the answer, and inside this list there is another list (in this case it is called "weather"), then how can I get the temperature ?
Here is an example of what I'm trying to accomplish 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'm trying to use that does not work, because I can only iterate over the first element:
try {
JsonFactory jfactory = new JsonFactory();
JsonParser jParser = jfactory.createJsonParser( new File("test.json") );
while ( jParser.nextToken() != JsonToken.END_OBJECT ) {
String fieldname = jParser.getCurrentName();
if ( "list".equals( fieldname ) ) {
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("-----------------");
source
share