简体   繁体   English

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

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

I have this list.json that I need to read to mapper object,我有这个列表。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"
        },          
}

I omit the long list, I only put two base or outter array, but basically its about 200 or more records.我省略了长列表,我只放了两个基本或外部数组,但基本上它大约有 200 条或更多记录。

The List.class,列表.class,

@Data
@AllArgsConstructor
@Entity
public class List {

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

Is the Code[] correct and also onetoone or onetomany? Code[] 是否正确,也是 onetoone 或 onetomany?

The Code.class,代码.class,

@Data
@AllArgsConstructor
@Entity
public class Code {

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

}

Do I need to put any string variable for csharp, java, pyhton?我是否需要为 csharp、java、pyhton 放置任何字符串变量? They key should be the same as the variable in the class?它们的键应该与 class 中的变量相同吗? But how do you do that since it's not constant?但是你怎么做,因为它不是恒定的?

There's a dynamic 2-layer json here in baeldung but how do I do that in the 3-layer? baeldung 有一个动态的 2 层json但是我如何在 3 层中做到这一点?

Here's I got, you have to use JsonNode for the rest of the layers.这是我得到的,你必须为层的 rest 使用 JsonNode。

I didn't use this annotation for now, don't want to struggle for now, just add getter/setter and constructor using fields, maybe something to do with java 8,我暂时没有使用这个注解,暂时不想挣扎,只是使用字段添加getter/setter和构造函数,可能与java 8有关,

@Data
@AllArgsConstructor
@Entity

@OneToOne(cascade = CascadeType.ALL)

So I remove it.所以我删除它。 Also how I did it, you have to simulate one by one in the json, meaning I have to add name and identity since those two are similar, if it works, then I add the code as JsonNode.另外我是怎么做的,你必须在json中一一模拟,这意味着我必须添加名称和身份,因为这两者相似,如果它有效,那么我将代码添加为JsonNode。

public class List {

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

Then on your controller,然后在您的 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"

Thanks to Baeldung for the idea.感谢Baeldung的想法。

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

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