JSON - 无法使用Jackson在Object中序列化JSONObject [英] JSON - Unable to serialize JSONObject within Object using Jackson
本文介绍了JSON - 无法使用Jackson在Object中序列化JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下课程:
class A{
String abc;
String def;
// appropriate getters and setters with JsonProperty Annotation
}
我打电话 Jacksons objectMapper.writeValueAsString(A)
哪个工作正常。
and I call Jacksons objectMapper.writeValueAsString(A)
which works fine.
现在我需要添加另一个实例成员:
Now I need to add another instance member:
class A{
String abc;
String def;
JSONObject newMember; // No, I cannot Stringify it, it needs to be JSONObject
// appropriate getters and setters with JsonProperty Annotation
}
但是当我序列化时,我得到例外:
but when I serialize, I am getting exception:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer
我尝试了JSONNode,但它将Output作为{outerjson: {innerjson} }而不是{outerjson:{innerjson}}。
I tried JSONNode but it gave Output as {outerjson:"{innerjson}"} not {outerjson:{innerjson}}.
是否可以使用Jackson实现上述输出,即JSONObject中的JSONObject?
Is it possible to use Jackson to achieve the above output, i.e. JSONObject within JSONObject?
推荐答案
使用JsonNode代替JSONObject。
Use JsonNode instead of JSONObject.
JsonNode jsonNode = JsonLoader.fromString(YOUR_STRING);
这篇关于JSON - 无法使用Jackson在Object中序列化JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文