对象转json字符串,属性首字母大写自动变为小写
作者:互联网
RegReply r = new RegReply();
Data d = new Data();
d.setDateTimestamp(date);
r.setMessageType("InfoReply");
r.setDeviceNo(DeviceNo);
r.setData(d);
String s = JSON.toJSONString(r);
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
实体类上打上注解@JSONField
package com.xw.elevator.platform.mqtt;
import com.alibaba.fastjson.annotation.JSONField;
public class RegReply {
@JSONField(name="MessageType")
private String MessageType;
@JSONField(name="DeviceNo")
private String DeviceNo;
@JSONField(name="Data")
private Data Data;
public String getMessageType() {
return MessageType;
}
public void setMessageType(String messageType) {
MessageType = messageType;
}
public String getDeviceNo() {
return DeviceNo;
}
public void setDeviceNo(String deviceNo) {
DeviceNo = deviceNo;
}
public Data getData() {
return Data;
}
public void setData(Data data) {
Data = data;
}
@Override
public String toString() {
return "RegReply [MessageType=" + MessageType + ", DeviceNo=" + DeviceNo + ", Data=" + Data + "]";
}
}
标签:JSONField,String,MessageType,首字母,json,小写,DeviceNo,Data,public 来源: https://blog.csdn.net/pb951028/article/details/104863866