繁体   English   中英

spring 引导 json 到 object 映射器与复杂的 json

[英]spring boot json to object mapper with complicated json

我有这个列表。json 我需要读取映射器 object,

{
    "name":"first", 
    "identity":"gold",
    "code":{
        "csharp":{
            "input":"sample of csharp code",
            "value":[ 
                {
                "main":"true",
                "power":"low"
                }, 
                {
                "main":"false",
                "power":"low"
                }
            ],
            "description":"description of csharp code",
            "manager":"bill gates"
        }, 
        "java":{
            "input":"sample of java",
            "value":[
                {
                "main":"true",
                "power":"low"
                },
                {
                "main":"false",
                "power":"high"
                },
                {
                "main":"true",
                "power":"low"
                }
            ], 
            "description":"description of java",
            "manager":"steve job"
        }
    }
},
{
    "name":"second", 
    "identity":"diamond",
    "code":{
        "python":{
            "input":"sample of python code",
            "new":"make it more complicated with new parm not value",  // do not forget this
            "description":"description of python code",
            "manager":"john doe"
        },
        "csharp":{
            "input":"sample of csharp code",
            "value":[ 
                {
                "main":"true",
                "power":"low"
                }, 
                {
                "main":"false",
                "power":"low"
                }
            ],
            "description":"description of csharp code",
            "manager":"bill gates"
        },          
}

我省略了长列表,我只放了两个基本或外部数组,但基本上它大约有 200 条或更多记录。

列表.class,

@Data
@AllArgsConstructor
@Entity
public class List {

    private String name;
    
    private String identity;
    
    @OneToOne(cascade = CascadeType.ALL)
    private Code[] code;
    
    public List() {}
}

Code[] 是否正确,也是 onetoone 或 onetomany?

代码.class,

@Data
@AllArgsConstructor
@Entity
public class Code {

    <<I have no idea what to put here>>

}

我是否需要为 csharp、java、pyhton 放置任何字符串变量? 它们的键应该与 class 中的变量相同吗? 但是你怎么做,因为它不是恒定的?

baeldung 有一个动态的 2 层json但是我如何在 3 层中做到这一点?

这是我得到的,你必须为层的 rest 使用 JsonNode。

我暂时没有使用这个注解,暂时不想挣扎,只是使用字段添加getter/setter和构造函数,可能与java 8有关,

@Data
@AllArgsConstructor
@Entity

@OneToOne(cascade = CascadeType.ALL)

所以我删除它。 另外我是怎么做的,你必须在json中一一模拟,这意味着我必须添加名称和身份,因为这两者相似,如果它有效,那么我将代码添加为JsonNode。

public class List {

    private String name;
    
    private String identity;
    
    JsonNode code;
    
    public List() {}
    
    // put getter/setter
    // put constractors as fields
}

然后在您的 controller 上,

    private String strJson = null;

    @PostConstruct
    private void loadData() {
        ClassPathResource classPathResource = new ClassPathResource("json/list.json");
        try {
            byte[] binaryData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
            strJson = new String(binaryData, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        DataModel datam = null;
        try {
            datam = objectMapper.readValue(strJson, List.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(datam.code()[0].get("csharp").get("value").get("main");  // output = "true"

感谢Baeldung的想法。

暂无
暂无

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

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