简体   繁体   English

使用jackson打开内部json对象

[英]unwrap inner json object using jackson

I've got a JSON that looks like this 我有一个看起来像这样的JSON

{
    "file": "sample.txt",
    "valid": "true",
    "parameters": {
         "size": "15kb",
         "charset": "UTF-8",
         ....
    }
}

But I want to deserialize it as a single object. 但我想将其反序列化为单个对象。 Not like this 不是这样的

class ValidatedFile {
    String file;
    boolean valid;
    FileParameters params;
}

but like this 但是像这样

class ValidatedFile {
    String file;
    boolean valid;
    String size;
    String charset;
    ....
}

I need to do some kind of unwrapping of this object. 我需要做一些这个对象的展开。 How to do it using jackson ? 怎么用jackson呢?

Use @JsonProperty("parameters") : 使用@JsonProperty("parameters")

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

public class Product {

    String file;
    boolean valid;
    String size;
    String charset;



    @JsonProperty("parameters")
    private void unpackNested(Map<String,Object> parameters) {
        this.size = (String)parameters.get("size");
        this.charset = (String)parameters.get("charset");
    }

}

Other approaches . 其他方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM