[英]ObjectMapper- Can not deserialize instance java.lang.Double
我想從Node.js發送的字符串中獲取該代碼的數據:
User[] user2 = mapper.readValue(resultPayload, 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;}
}
但我收到這樣的警告:
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
我的JSON字符串包含:
[{"locationLon":{"N":"16.946397721767426"},"locationLat":{"N":"52.447558225994904"}},{"locationLon":{"N":"16.88841037452221"},"locationLat":{"N":"52.44599882989592"}},{"locationLon":{"N":"16.94861490279436"},"locationLat":{"N":"52.44514319230585"}}]
第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;
}
}
第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 + "]";
}
}
第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));
請考慮進行上述所有更改並查看結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.