简体   繁体   English

如何编写Java类以从没有固定字段的Json获取对象?

[英]How to write Java class to fetch the object from Json which doesn't have fixed fields?

I need to have Java class to accommodate below requirement and it should be compatible for Jackson's parsing using Object Mapper. 我需要具有Java类来满足以下要求,并且该类应与使用Object Mapper的Jackson解析兼容。

The Json is in below format : Json的格式如下:

[
 { 
   "name" : "Snehal",
   "property1" : "value11",
   "property2" : "value12",
   "property3" : "value13",

 },
 {
   "name" : "Masne",
   "property1" : "value21",
   "property2" : "value22",
   "property3" : "value23",

},
]

In above Json, the no. 在Json以上,没有。 of properties are not fixed, meaning there can be, say, property4, 5, 6, etc 属性的数量不是固定的,也就是说可以有property4、5、6等

The corresponding Java class could be thought of as below : 可以考虑以下对应的Java类:

Class MyClass
{

  String name;

  List<String> properties;

 // getters, setters, etc

}

But this wont solve the purpose since, in this case, Json will generated something like of below format: 但这无法解决目的,因为在这种情况下,Json将生成类似以下格式的内容:

[ 
 {
   "name" : "Snehal",
   [
      {"property" : "value1" },
      {"property" : "value1" },
      {"property" : "value1" }
   ]
 },

 {
   .... []
 }

]

How do I implement the Java class to achieve the data in specifeid Json format ? 如何实现Java类以实现特定Json格式的数据?

You can use @JsonAnyGetter/@JsonAnySetter annotations to mark that your class has 'extra' properties in addition to the declared fields. 您可以使用@ JsonAnyGetter / @ JsonAnySetter批注来标记您的类除声明的字段外还具有“额外”属性。

Here is an example: 这是一个例子:

public class JacksonAnyGetter {

    static final String JSON = " { \n" +
            "   \"name\" : \"Snehal\",\n" +
            "   \"property1\" : \"value11\",\n" +
            "   \"property2\" : \"value12\",\n" +
            "   \"property3\" : \"value13\"\n" +
            "\n" +
            " }";

    static class Bean {
        public String name; // we always have name
        private Map<String, Object> properties = new HashMap<>();

        @JsonAnySetter
        public void add(String key, String value) {
            properties.put(key, value);
        }

        @JsonAnyGetter
        public Map<String, Object> getProperties() {
            return properties;
        }

        @Override
        public String toString() {
            return "Bean{" +
                    "name='" + name + '\'' +
                    ", properties=" + properties +
                    '}';
        }
    }

    public static void main(String[] args) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        final Bean bean = mapper.readValue(JSON, Bean.class);
        System.out.println(bean);
        final String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
        System.out.println(json);
    }
}

Output: 输出:

Bean{name='Snehal', properties={property3=value13, property2=value12, property1=value11}}
{
  "name" : "Snehal",
  "property3" : "value13",
  "property2" : "value12",
  "property1" : "value11"
}

暂无
暂无

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

相关问题 如何为 Java 库中没有 apply 方法的对象编写 JSON 格式? - How do I write a JSON Format for an object in the Java library that doesn't have an apply method? 如何将JSON反序列化为已知必填字段但可能有多个未知字段的Java类? - How can I deserialize a JSON to a Java class which has known mandatory fields, but can have several unknown fields? 如何根据JSON对象的字段值创建特定Java类的对象? - How to create an object of specific Java class from JSON object depending on it's fields values? Java-从对象获取具有公共超类的字段列表 - Java - Getting a list of fields that have a common super class from an object 如何从另一个没有包名的类加载器中加载一个类? - How to load a class from another classloader which doesn't have a package name? 如何编写一个引用类的构造函数,该类指向Java中的对象? - How to write a constructor that takes a reference to a class, which points to an object in Java? 如何从Kotlin数据类创建对象的Java实例,但不包括所有字段? - How to create a Java instance of the object from Kotlin data class but don't include all fields? 如何在不使用Java中的Class创建Object的情况下获取MySQL数据 - How to fetch MySQL data without creating Object from Class in Java 如何使用gson将选定的字段从JSON映射到Java对象 - How to map selected fields from JSON to Java Object using gson 为什么java.lang.Thread类没有一个仅包含ThreadGroup的构造函数? - Why doesn't java.lang.Thread class have a constructor which takes only a ThreadGroup in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM