[英]Nested JSONObject Deserialize to JSONObject
So I'm working on deserializing a nested JSONObject, but don't want to create a class for each nested object. 因此,我正在反序列化嵌套的JSONObject,但不想为每个嵌套的对象创建一个类。 I was trying to take on of the nested JSONObjects and put it in a JSONObject. 我试图使用嵌套的JSONObjects并将其放入JSONObject中。
public class ContainerStatus {
@JsonProperty("name")
private String name;
@JsonProperty("state")
private JSONObject state;
@JsonProperty("lastState")
private JSONObject lastState;
@JsonProperty("ready")
private Boolean ready;
@JsonProperty("restartCount")
private Integer restartCount;
@JsonProperty("image")
private String image;
@JsonProperty("imageID")
private String imageID;
@JsonProperty("containerID")
private String containerID;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
Using this to deserialize: 使用这个来反序列化:
{ "containerStatuses":
{
"name": "connect",
"state": {
"terminated": {
"exitCode": 1,
"reason": "Error",
"startedAt": "2019-03-20T15:40:08Z",
"finishedAt": "2019-03-20T15:40:50Z",
"containerID": "docker://"
}
},
"lastState": {},
"ready": true,
"restartCount": 0,
"image": "image",
"imageID": "docker-pullable://",
"containerID": "docker://"
}}
I get unrecognized field "terminated", because of the state JSONObject. 由于状态JSONObject,我无法识别字段“终止”。
I want a: 我想要一个:
JsonObject state = { "terminated": { "exitCode": 1, "reason": "Error", "startedAt": "2019-03-20T15:40:08Z", "finishedAt": "2019-03-20T15:40:50Z", "containerID": "docker://" } }
I can cast it into a generic object, but the format isn't JSON anymore: 我可以将其转换为通用对象,但格式不再是JSON:
@JsonProperty("state")
private Object state;
Gives me this format:
{running={startedAt=2019-03-20T14:53:53Z}}
You need to improve a little bit your example: 您需要改进一下示例:
DeserializationFeature.UNWRAP_ROOT_VALUE
, enable this feature to unwrap JSON
object. DeserializationFeature.UNWRAP_ROOT_VALUE
,启用此功能可解包装JSON
对象。 JsonRootName
annotation to your POJO
class because class name does not match to property containerStatuses
. 将JsonRootName
批注添加到POJO
类中,因为类名与属性containerStatuses
不匹配。 JsonNode
which comes from Jackson
library instead of JSONObject
which comes probably from org.json
. 使用来自Jackson
库的JsonNode
代替可能来自org.json
的JSONObject
。 Improved example could look like below: 改进的示例如下所示:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonApp {
public static void main(String[] args) throws Exception {
String json = "{...}";
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
ContainerStatus cs = mapper.readValue(json, ContainerStatus.class);
System.out.println(cs.getState());
}
}
@JsonRootName("containerStatuses")
class ContainerStatus {
@JsonProperty("name")
private String name;
@JsonProperty("state")
private JsonNode state;
@JsonProperty("lastState")
private JsonNode lastState;
@JsonProperty("ready")
private Boolean ready;
@JsonProperty("restartCount")
private Integer restartCount;
@JsonProperty("image")
private String image;
@JsonProperty("imageID")
private String imageID;
@JsonProperty("containerID")
private String containerID;
// getters, setters, toString
}
Above code prints: 上面的代码打印:
{"terminated":{"exitCode":1,"reason":"Error","startedAt":"2019-03-20T15:40:08Z","finishedAt":"2019-03-20T15:40:50Z","containerID":"docker://"}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.