如何在 Spring Boot Java 中为动态 JSON 结构创建类

问题描述

我有以下 JSON 结构作为输入,可以嵌套也可以不嵌套。我想在 Spring Boot 应用程序中获取 JSON 作为输入和处理。如何在 JSON 中创建一个动态键值的类。它可以是 JSON 输入中的任何键值对。以下是示例。

无嵌套:

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

嵌套:

{
    "mappings": {
        "properties": {
            "firstname": {
                "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"
                    }
                }
            }
        }
    }
}

我不知道如何创建一个类来支持嵌套和不嵌套在单个类中。我尝试了以下方法。它没有帮助。

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,Object>> getField() {
            return field;
        }

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)