Jackson反序列化包含有效json的字符串值

时间:2021-03-22 20:28:12

Suppose I have the following JSON data:

假设我有以下JSON数据:

{
    "header": "some value",
    "message": "{\"field1\": \"abc\", \"field2\": 123}"
}

Is it possible to adjust the annotations on OuterClass so the message field will be parsed as an InnerClass object?

是否可以在OuterClass上调整注释,以便将消息字段解析为InnerClass对象?

public class InnerClass {
    @JsonProperty("field1")
    public void setField1(String value) {/* do stuff */}

    @JsonProperty("field2")
    public void setField2(Integer value) {/* do stuff */}
}

public class OuterClass {
    @JsonProperty("message")
    public void setMessage(InnerClass obj) {/* do stuff */}  
}

Ideally I would like the calling code to look something like:

理想情况下,我希望调用代码看起来像:

ObjectMapper mapper = new ObjectMapper();
OuterClass obj = mapper.readValue(jsonStr, OuterClass.class);

2 个解决方案

#1


3  

Structure your outer class to have a property of the other class that represents the nested JSON, like:

构造外部类以具有表示嵌套JSON的另一个类的属性,如:

public class OuterClass {

    @JsonProperty("header")
    private String header; 

    @JsonProperty("message")
    private InnerClass message; 

    //getters & setters
}

#2


0  

Once I added some complexity, the accepted answer would not work. I kept getting the error:

一旦我增加了一些复杂性,接受的答案将无效。我一直收到错误:

Can not instantiate value of type [simple type, InnerClass] from JSON String; no single-String constructor/factory method (through reference chain: InnerClass)

无法从JSON String实例化[simple type,InnerClass]类型的值;没有单字符串构造函数/工厂方法(通过引用链:InnerClass)

I ended up using the following approach

我最终使用了以下方法

public class OuterClass {
    public InnerClass message;

    @JsonCreator
    public OuterClass (Map<String,Object> delegate) throws IOException {
        String json = (String)delegate.get("Message");
        ObjectMapper mapper = new ObjectMapper();
        this.message = mapper.readValue(json, InnerClass.class);
     }
}

#1


3  

Structure your outer class to have a property of the other class that represents the nested JSON, like:

构造外部类以具有表示嵌套JSON的另一个类的属性,如:

public class OuterClass {

    @JsonProperty("header")
    private String header; 

    @JsonProperty("message")
    private InnerClass message; 

    //getters & setters
}

#2


0  

Once I added some complexity, the accepted answer would not work. I kept getting the error:

一旦我增加了一些复杂性,接受的答案将无效。我一直收到错误:

Can not instantiate value of type [simple type, InnerClass] from JSON String; no single-String constructor/factory method (through reference chain: InnerClass)

无法从JSON String实例化[simple type,InnerClass]类型的值;没有单字符串构造函数/工厂方法(通过引用链:InnerClass)

I ended up using the following approach

我最终使用了以下方法

public class OuterClass {
    public InnerClass message;

    @JsonCreator
    public OuterClass (Map<String,Object> delegate) throws IOException {
        String json = (String)delegate.get("Message");
        ObjectMapper mapper = new ObjectMapper();
        this.message = mapper.readValue(json, InnerClass.class);
     }
}