I have such a program that get data using API REST and show it in the JTextArea. Everything is OK but I need to modify this program with deserializing. I need to deserialized data which I get from the API to the class Country and only then show it. I tried but got the error:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of model.Country[] out of START_OBJECT token
at [Source: {"nativeName":"latitude","longitude"}; line: 1, column: 1]
Here is my main class (my attempt to do deserializing is commented):
public void actionPerformed(ActionEvent evt) {
/*ObjectMapper mapper = new ObjectMapper();
String userDataJSON = "{\"nativeName\":\"latitude\",\"longitude\"}";
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
try {
Country[] userFromJSON = mapper.readValue(userDataJSON, Country[].class);
System.out.println(userFromJSON);
} catch (IOException e1) {
System.out.println(e1);
}*/
...
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
} catch (Exception e) {
}
}
}
And my class Country:
public class Country {
@JsonProperty
private String name;
@JsonProperty
public String nativeName;
@JsonProperty
private String latitude;
@JsonProperty
private String longitude;
@JsonProperty
private String currencyCode;
public Country(String name, String nativeName, String latitude, String longitude, String currencyCode) {
super();
this.name = name;
this.nativeName = nativeName;
this.latitude = latitude;
this.longitude = longitude;
this.currencyCode = currencyCode;
// getters & setters
}