我有一个JSON clob,我将其反序列化为bean,然后将其作为HTTP请求的响应发送为JSON。
我想在包含任意JSON的bean中添加一个JSONObject字段。
这是我的豆子:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DTO implements Serializable {
private String height;
private String width;
//I would like to add this field
//private JSONObject settings;
以下是我如何反序列化CLOB:
DTO layout = null;
String json = "{\"height\": \"300px\", \"width\": \"100%\"}";
final ObjectMapper mapper = jackson.getContext(DTO.class);
try {
layout = mapper.readValue(json, DTO.class);
}
catch(IOException ex) {
logger.error("Unable to deserialize Dashboard layout CLOB", ex);
}
这很有效。但是,我需要我的clob包含一个包含任意JSON的设置对象。这可能吗?例如:
{"height": "300px", "width": "100%", "settings": {"propA": 1, "propB": "red"}}
当我使用JSONObject尝试此操作时,我收到错误:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.codehaus.jettison.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: api.jaxb.DTO["settings"])
答案 0 :(得分:2)
JSONObject
来自JSON library,这是一个独立的包,而不是杰克逊。在您的情况下,您应该使用杰克逊图书馆的JsonNode。:
public class DTO implements Serializable {
...
private org.codehaus.jackson.JsonNode settings;