象属性是驼峰的,json中key是下划线,使用JsonField注解,不起作用,在对象转json的过程中,对象中是驼峰的属性全部丢失了
封装的接收类为
import org.nutz.json.JsonField;
/**
* 单个设备状态
*
* @datetime 2020/7/21 14:18
*/
public class DeviceStatus2 {
/**
* 设备id,通过查询数据库获取
*/
@JsonField(value = "device_id")
private String deviceId;
/**
* 设备状态 当值为'OK'时,表示没问题
*/
@JsonField(value = "status")
private String status;
/**
* 状态描述
*/
@JsonField(value = "status_str")
private String statusStr;
public DeviceStatus2() {
}
public DeviceStatus2(String deviceId, String status, String statusStr) {
this.deviceId = deviceId;
this.status = status;
this.statusStr = statusStr;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatusStr() {
return statusStr;
}
public void setStatusStr(String statusStr) {
this.statusStr = statusStr;
}
}
测试
String json = "{\"data\":[{\"device_id\":\"sssss21\",\"status\":\"Fail\",\"status_str\":\"失败到达功\"},{\"device_id\":\"sssss11\",\"status\":\"OK\",\"status_str\":\"ce时成功\"}]}";
System.out.println("原json----" + json);
NutMap requestMap = Json.fromJson(NutMap.class, json);
System.out.println("1----" + Json.toJson(requestMap,JsonFormat.tidy()));
List<DeviceStatus2> dataList = requestMap.getAsList(PoConsts.DEVICE_STATUS_DATA, DeviceStatus2.class);
System.out.println("2----" + Json.toJson(dataList,JsonFormat.tidy()));
返回结果如下
原json----{"data":[{"device_id":"sssss21","status":"Fail","status_str":"失败到达功"},{"device_id":"sssss11","status":"OK","status_str":"ce时成功"}]}
1----{"data":[{"device_id":"sssss21","status":"Fail","status_str":"失败到达功"}, {"device_id":"sssss11","status":"OK","status_str":"ce时成功"}]}
2----[{"device_id":null,"status":"Fail","status_str":null}, {"device_id":null,"status":"OK","status_str":null}]