gpt4 book ai didi

JAVA json架构2 POJO

转载 作者:行者123 更新时间:2023-11-30 02:30:52 25 4
gpt4 key购买 nike

我必须创建 REST 响应。数据为 json 格式,结构必须如下:

{
"device_id" : { "downlinkData" : "deadbeefcafebabe"}
}

“device_id”必须替换为DeviceId,例如:

{
"333ee" : { "downlinkData" : "deadbeefcafebabe"}
}

{
"9886y" : { "downlinkData" : "deadbeefcafebabe"}
}

我用了http://www.jsonschema2pojo.org/这就是结果:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"device_id"
})
public class DownlinkCallbackResponse {

@JsonProperty("device_id")
private DeviceId deviceId;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("device_id")
public DeviceId getDeviceId() {
return deviceId;
}

@JsonProperty("device_id")
public void setDeviceId(DeviceId deviceId) {
this.deviceId = deviceId;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"downlinkData"
})
public class DeviceId {

@JsonProperty("downlinkData")
private String downlinkData;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("downlinkData")
public String getDownlinkData() {
return downlinkData;
}

@JsonProperty("downlinkData")
public void setDownlinkData(String downlinkData) {
this.downlinkData = downlinkData;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

但是基于这个 POJO,我无法设置 deviceID:

DownlinkCallbackResponse downlinkCallbackResponse = new DownlinkCallbackResponse ();

DeviceId deviceId = new DeviceId();
deviceId.setDownlinkData(data);
downlinkCallbackResponse.setDeviceId(deviceId);

return new ResponseEntity<>(downlinkCallbackResponse, HttpStatus.OK);

最佳答案

获取以下 json 字符串

 { "downlinkData" : "deadbeefcafebabe"}

创建 json 对象(库:java-json.jar)

 JSONObject obj = new JSONObject();

将上面的json字符串放入json对象中。

 obj.put("333ee", jsonString);

这将创建以下 json 字符串

{

"333ee" : { "downlinkData" : "deadbeefcafebabe"}
}

希望这对您有帮助。 :-)

关于JAVA json架构2 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44292729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com