[英]Parsing JSON in java - how to get String or JsonObject from json file property(value - json object) using POJO only
json结构如下:
{
"key":"Key",
"value":{
"first": "first",
"second": "second"
}
}
我想使用Spring Boot将此json作为Plain Old Java Object获取 。
public class File {
private String key;
private JsonObject value;
public File(String key, JsonObject value) {
this.key = key;
this.value = value;
}
public File() {
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public JsonObject getValue() {
return value;
}
public void setValue(JsonObject value) {
this.value = value;
}
}
JsonObject - lib com.google.gson中的类
我正在{}
将字段“值”获取为String会是完美的,但是JsonObject也可以。
完成! 使用setter的com.google.json lib是可能的:
public class File {
private String key;
private String value;
public File() {
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(Object value) {
this.value = new Gson().toJson(value);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.