如何解决如何在 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;
}
}
}
解决方法
我遇到过类似的情况,我的 JSON 可能没有一致的键值对。 我在类级别给出了以下 jackson 注释,以便我的模型中不可用和 JSON 中存在的任何属性都将被忽略。
param(
$firstDirectoryName = "D:\tmp\001",$SecondDirectoryName = "D:\tmp\002"
)
$firstList = Get-ChildItem $firstDirectoryName -File -Recurse | ForEach-Object {
[PSCustomObject]@{
relativePath = $_.FullName.TrimStart($firstDirectoryName)
hash = (Get-FileHash $_.FullName -Algorithm MD5).Hash
}
}
$secondList = Get-ChildItem $SecondDirectoryName -File -Recurse | ForEach-Object {
[PSCustomObject]@{
relativePath = $_.FullName.TrimStart($SecondDirectoryName)
hash = (Get-FileHash $_.FullName -Algorithm MD5).Hash
}
}
Compare-Object -ReferenceObject $firstList -DifferenceObject $secondList -Property relativePath,hash
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。