[英]Json Jackson do not deserialize inner object (null on properties)
I know there was alot of posts like this but still I am not able to find an proper anwser. 我知道有很多这样的帖子,但是我仍然找不到合适的答案。
I need to parse given Json from web 我需要从网络解析给定的Json
{
"args": {
},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "Dalvik/1.6.0 (Linux; U; Android 4.3; C5303 Build/12.1.A.1.205)"
},
"origin": "91.193.147.8",
"url": "https://httpbin.org/get"
}
I have 2 classes 我有2节课
first one holds all information got from website 第一个保存从网站获得的所有信息
public class WebJSONObject {
@JsonDeserialize(using = HeadersDeserializer.class)
private Headers headers;
private String origin;
private String url;
//getters and seters
}
it includes Headers class which look like this: 它包含Headers类,如下所示:
@JsonRootName("headers")
public class Headers {
@JsonProperty("Accept")
private String accept;
@JsonProperty("Accept-Encoding")
private String acceptEncoding;
@JsonProperty("Content-Length")
private String contentLenght;
@JsonProperty("Host")
private String host;
@JsonProperty("User-Agent")
private String userAgent;
//Getters and setters
}
properties like origin and url are parsed properly but there is an issue with Header class 正确解析了诸如origin和url之类的属性,但Header类存在问题
Here is my derserializer 这是我的解串器
public class HeadersDeserializer extends JsonDeserializer<Headers> {
@Override
public Headers deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
JsonNode node = mapper.readTree(jsonParser);
//for testing issue (parsing properly)
//Json String: {"origin":"91.193.147.8","url":"https://httpbin.org/get"}
TreeNode tree = jsonParser.getCodec().readTree(jsonParser);
String node_as_text2 = tree.toString();
WebJSONObject wbo = mapper.readValue(node_as_text2,WebJSONObject.class);
//Json String {"Accept":"application/json","Accept-Encoding":"gzip","Content-Length":"0","Host":"httpbin.org","User-Agent":"Dalvik/1.6.0 (Linux; U; Android 4.3; C5303 Build/12.1.A.1.205)"}
String node_as_text = mapper.writeValueAsString(node);
Headers headers = mapper.readValue(node_as_text,Headers.class);
return headers;
}
}
Also here when i do test-parse of WebJsonObject its done well (origin and url are filled with valuse) Problem is when i Parse Headers, all properties are filled with null. 同样在这里,当我对WebJsonObject进行测试解析时,它做得很好(原始值和url用值填充)问题是当我解析标头时,所有属性都填充为null。
When you use @JsonRootName("headers")
you're assuming that you'll parse the whole json into a Headers object. 当您使用
@JsonRootName("headers")
您假设将整个json解析为Headers对象。
Forgiving my bad memory, it'd work if you wrote something like this: 原谅我的美好记忆,如果您编写了以下内容,它会起作用:
Headers headers = objectMapper.readValue(string,Headers.class);
But since you mention that you're parsing origin and url correctly you're implying that you did something like 但是由于您提到要正确解析来源和网址,因此就意味着您做了类似的事情
WebJSONObject object = objectMapper.readValue(string, WebJSONObject.class);
If that's correct the solution is about dropping both @JsonRootName
and @JsonDeserialize
. 如果正确,则解决方案是同时删除
@JsonRootName
和@JsonDeserialize
。
In fact it's very unusual to use either of those annotations, or to write a deserializer from scratch. 实际上,使用这些注释或从头开始编写反序列化器是非常不寻常的。 They're only needed for edge cases where there's not a direct correspondance between json and POJO's, and even in a lot of those cases you can get away with other features like UNWRAP_ROOT.
仅在json和POJO之间没有直接对应关系的极端情况下才需要使用它们,即使在许多情况下,您也可以使用UNWRAP_ROOT之类的其他功能。
I have solved problem. 我已经解决了问题。 Which was unrelated to code itself.
这与代码本身无关。
Wrong thing was that I imported 我进口错了
import org.codehaus.jackson.annotate.JsonProperty;
instead of 代替
import com.fasterxml.jackson.annotation.JsonProperty
... ...
Code is ok despite fact that (as @Fabio said) there is no need to write any Deserializer. 尽管事实上(如@Fabio所说)无需编写任何反序列化器,但代码尚可。
One can simply do: 一个人可以简单地做:
public class WebJSONObject {
private Headers headers;
private String origin;
private String url;
//setters and getters
public class Headers {
@JsonProperty("Accept")
private String accept;
@JsonProperty("Accept-Encoding")
private String acceptEncoding;
@JsonProperty("Content-Length")
private String contentLenght;
@JsonProperty("Host")
private String host;
@JsonProperty("User-Agent")
private String userAgent;
//setters and getters
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.