对象
public class GpsPoi {
private String address;
private String latitude;
private String longitude;
private String mobileNumber;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
转化
public Object Object2Json() {
ArrayList list = new ArrayList();
GpsPoi poi = new GpsPoi();
poi.setAddress("广州");
poi.setLatitude("23");
poi.setLongitude("113");
poi.setMobileNumber("18912345678");
list.add(poi);
StringWriter str=new StringWriter();
ObjectMapper objectMapper=new ObjectMapper();
try {
objectMapper.writeValue(str, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ServerResponse(0, str.toString());
}
public Object Json2Object() {
String str="{\"address\":\"广州\",\"latitude\":\"23\",\"longitude\":\"113\",\"mobileNumber\":\"18912345678\"}";
ObjectMapper objectMapper=new ObjectMapper();
GpsPoi poi = new GpsPoi();
try {
poi = objectMapper.readValue(str, GpsPoi.class);
} catch (IOException e) {
e.printStackTrace();
}
return new ServerResponse(0, poi.getAddress());
}