简体   繁体   English

对于包含列表的 Java 类,如何从 json 字符串中获取对象,使用 Immutables 生成?

[英]How to get object from json string, for Java class containing a list, generated using Immutables?

my use case included a similar class, using immutables ( https://immutables.github.io/ ) :我的用例包括一个类似的类,使用不可变( https://immutables.github.io/ ):

@Value.Immutable
@Gson.TypeAdapters
public abstract class SampleClass {
    public abstract String var1();
    public abstract String var2();
    public abstract Date date1();
}

I was converting it to Json string,using gson, and then getting the object again using :我正在使用 gson 将其转换为 Json 字符串,然后使用以下命令再次获取对象:

SampleClass obj1 = new Gson().fromJson("generated_json_string",ImmutableSampleClass.class);

But now I had to change var1 to List of String, and now I am getting :但是现在我不得不将 var1 更改为字符串列表,现在我得到了:

java.lang.RuntimeException: Failed to invoke com.google.common.collect.ImmutableList() with no args

What is the correct way to get the object from JSON string ?从 JSON 字符串获取对象的正确方法是什么?

Figured out what was wrong, hence answering the question.弄清楚出了什么问题,因此回答了这个问题。 Immutables generated class GsonAdaptersSampleClass, which implements TypeAdapterFactory.不可变生成类 GsonAdaptersSampleClass,它实现了 TypeAdapterFactory。 Using this and with the help of this answer : https://stackoverflow.com/a/13624060/3192744使用这个并在这个答案的帮助下: https : //stackoverflow.com/a/13624060/3192744

I could find the following correct way to deserialize JSON string:我可以找到以下反序列化 JSON 字符串的正确方法:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersSampleClass());
Gson gson = gsonBuilder.create();
SampleClass obj1 = gson.fromJson("generated_json_string",ImmutableSampleClass.class);

You can try GSON library for converting JSON to your existing class object.您可以尝试使用 GSON 库将 JSON 转换为现有的类对象。

http://howtodoinjava.com/best-practices/google-gson-tutorial-convert-java-object-to-from-json/ http://howtodoinjava.com/best-practices/google-gson-tutorial-convert-java-object-to-from-json/

暂无
暂无

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

相关问题 使用Immutables时,从接口到生成的java类进行注释 - Carry forward annotation from interface to generated java class when using Immutables 如何使用 Java 将包含 JSON 对象的字符串转换为实际的 JSON - How to convert String containing JSON object to actual JSON using Java 如何从Java中包含诸如&#39;:&#39;,&#39;[&#39;和&#39;]&#39;等字符的String创建Json对象 - How to create Json object from String containing characters like ':' ,'[' and ']' in Java 使用 Class<t> 与不可变</t> - Using Class<T> with immutables Java-从包含父类的对象获取扩展类 - Java - get extended class from object containing parent class 如何获得清单 <Long> Java中使用FlexJson Json序列化程序从json字符串中提取数据? - How to get List<Long> from json string using FlexJson Json serializer in Java? 从Json对象列表中获取字符串列表 - Get list of string from list of Json object How to get a JSON object from a file and map it to a Java class using Apache Camel routes? - How to get a JSON object from a file and map it to a Java class using Apache Camel routes? 如何在 Java 8 中使用 reduce 从 Object 列表中创建字符串列表? - How to create a list of String from a list of Object using reduce in Java 8? 在Java中解析JSON-如何仅使用POJO从json文件属性(值-json对象)中获取String或JsonObject - Parsing JSON in java - how to get String or JsonObject from json file property(value - json object) using POJO only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM