I want to get data from the string send by Node.js with that code:
User[] user2 = mapper.readValue(resultPayload, User[].class);
My User class:
public class User {
private double lat;
private double lon;
public User() {
}
public User(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public double getLocationLat(){return lat;}
public void setLocationLat(double lat){this.lat = lat;}
public double getLocationLon(){return lon;}
public void setLocationLon(double lon){this.lon = lon;}
}
But I get such an warning:
WARNING: could not load Java7 Path class
/com.amazon.mysampleapp W/System.err: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Double out of START_OBJECT token
My JSON string contains:
[{"locationLon":{"N":"16.946397721767426"},"locationLat":{"N":"52.447558225994904"}},{"locationLon":{"N":"16.88841037452221"},"locationLat":{"N":"52.44599882989592"}},{"locationLon":{"N":"16.94861490279436"},"locationLat":{"N":"52.44514319230585"}}]
Step 1: Location.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class Location {
private double N;
public Location() {
}
public Location(double n) {
N = n;
}
@JsonProperty("N")
public double getN() {
return N;
}
public void setN(double n) {
N = n;
}
@Override
public String toString() {
return "N=" + N;
}
}
Step 2: User.java
public class User {
private Location locationLat;
private Location locationLon;
public User() {
}
public User(Location locationLat, Location locationLon) {
super();
this.locationLat = locationLat;
this.locationLon = locationLon;
}
public Location getLocationLat() {
return locationLat;
}
public void setLocationLat(Location locationLat) {
this.locationLat = locationLat;
}
public Location getLocationLon() {
return locationLon;
}
public void setLocationLon(Location locationLon) {
this.locationLon = locationLon;
}
@Override
public String toString() {
return "User [locationLat=" + locationLat + ", locationLon=" + locationLon + "]";
}
}
Step 3:
String resultPayload=new String(Files.readAllBytes(new File("D:/test3.json").toPath()));
ObjectMapper mapper = new ObjectMapper();
User[] users = mapper.readValue(resultPayload, User[].class);
System.out.println(Arrays.toString(users));
Please consider making all of the above changes and see the result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.