I have the following json file:
[{"name":"James","height":170.3,"weight":55.945535152517735,"items":[{"name":"Tickle","number":1000},{"name":"Greedy","number":1400}]},{"name":"Mary","height":150.43,"weight":55.943284737579376,"items":[{"name":"Happy","number":1400},{"name":"Nosey","number":1100}]},{"name":"Robert","height":167.21,"weight":55.94449876875712,"items":[{"name":"Sneeze","number":1400},{"name":"Bump","number":900}]},{"name":"John","height":134.32,"weight":55.94390696616939,"items":[{"name":"Snow","number":1400},{"name":"Messy","number":900}]}]
And have two classes for the data:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class People {
private String name;
private double height;
private double weight;
private Item item;
//setters and getters
}
...
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item{
private String name;
private int number;
//setters and getters
}
I used jackson to read the data:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class TestClient {
public static void main(String[] args)
{
ObjectMapper mapper = new ObjectMapper();
try {
People[] usrPost = mapper.readValue(new URL("https://mylink"), People[].class);
System.out.println(usrPost[2]);
} catch ( MalformedURLException e ) {
e . printStackTrace ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
For some reason I keep on getting the following error message:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type ` ` from Array value (token `JsonToken.START_ARRAY`)
at [Source: (URL); line: 1, column: 97] (through reference chain: java.lang.Object[][0]-> People["item"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1462)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:648)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:211)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:314)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:214)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:24)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3560)
at uk.ac.ed.inf.TestClient.main(TestClient.java:21)
Any help would be greatly appreciated! Think there might be an issue with the second Item class and the fact that I'm reading an array.
CodePudding user response:
try this modified People class ... in your json, items is an array, so model must fit
@JsonIgnoreProperties(ignoreUnknown = true)
public class People {
private String name;
private double height;
private double weight;
private List<Item> items;
// other getter/setter
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}