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 + "]";
}
}