简体   繁体   English

如何在 Spring 启动 ZD52387880E1EA22817A8972D375921 中为动态 JSON 结构创建 class

[英]How to create class for dynamic JSON structure in Spring Boot Java

I have following JSON structure as input which can be nested and without nested.我有以下 JSON 结构作为可以嵌套和不嵌套的输入。 I want to fetch the JSON as input and process in Spring Boot application.我想获取 JSON 作为 Spring 引导应用程序中的输入和处理。 How to create a class which dynamic key values in the JSON.如何在 JSON 中创建一个 class 动态键值。 It can be any key-value pairs in the JSON input.它可以是 JSON 输入中的任何键值对。 Below is the sample one.下面是示例之一。

Without Nested:没有嵌套:

{
    "mappings": {
        "properties": {
            "firstname": {
                "type": "string"
            },
            "lastname": {
                "type": "string"
            },
            "salary": {
                "type": "integer"
            },
            "date_of_birth": {
                "type": "date"
            }
        }
    }
}

With Nested:嵌套:

{
    "mappings": {
        "properties": {
            "firstname": {
                "type": "string"
            },
            "lastname": {
                "type": "string"
            },
            "annual_salary": {
                "type": "integer"
            },
            "date_of_birth": {
                "type": "date"
            },
            "comments": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "comment": {
                        "type": "string"
                    },
                    "age": {
                        "type": "short"
                    },
                    "stars": {
                        "type": "short"
                    },
                    "date": {
                        "type": "date"
                    }
                }
            }
        }
    }
}

I don't know how to create a class to support both with and without nested in single class.我不知道如何创建一个 class 来支持嵌套和不嵌套在单个 class 中。 I have tried the following.我尝试了以下方法。 It doesn't help.它没有帮助。

public class Schema {
    Mapping mappings;

    public Mapping getMappings() {
        return mappings;
    }

    public void setMappings(Mapping mappings) {
        this.mappings = mappings;
    }

    public static class Mapping {
        Property properties;

        public Property getProperties() {
            return properties;
        }

        public void setProperties(Property properties) {
            this.properties = properties;
        }
    }

    public static class Property {
        Map<String, Map<String, Object>> field = new HashMap<>();

        public Map<String, Map<String, Object>> getField() {
            return field;
        }

        public void setField(Map<String, Map<String, Object>> field) {
            this.field = field;
        }
    }
}

I have come across a scenario similar to this where it was possible that my JSON may not be having consistent Key-Value pairs.我遇到了与此类似的情况,我的 JSON 可能没有一致的键值对。 I gave the following jackson annotation at class level so that whichever property not available in my model and which are present in the JSON will be ignored. I gave the following jackson annotation at class level so that whichever property not available in my model and which are present in the JSON will be ignored.

@JsonIgnoreProperties(ignoreUnknown = true)

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

相关问题 如何在Spring Boot中将map JSON数组转为Java class - How to map JSON array to Java class in Spring Boot 如何创建一个包含多个对象的json数组 java spring boot - How to create a json array with multiple objects java spring boot 如何在Spring Boot中使用Java从JSON响应中提取特定部分,并更改JSON结构中的字段名称(如本例所示)? - How to extract a specific part from JSON response with changing fields names in JSON structure (as in this example) with Java in Spring Boot? 如何在Java中创建json结构? - how to create json structure in java? 如何在 Spring 引导中生成动态 Json - how to generate dynamic Json in Spring boot 如何在Java中创建动态树数据结构 - How to create dynamic tree data structure in Java 将带有密钥动态的 json 转换为 POJO .. java spring 引导 - Convert json with key dynamic to POJO.. java spring boot 当 json 请求中不存在以下属性时,如何在 json 引导 java 中创建 json 响应如下所示 - how to create json response in spring boot java like this as shown when the following attributes are not present in json request 如何 map JSON object 和 java Pojo class 忽略父 class spring 启动? - How to map JSON object with java Pojo class ignoring parent class spring boot? 如何在Java Spring Boot中记录JSON对象? - How to log JSON object in java spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM