Gson -> 导入包含对象的 JSON

huangapple go评论82阅读模式
英文:

Gson -> importing a JSON with objects

问题

我正在尝试使用Gson来导入一个我创建的类结构的JSON文件。但是,尽管toJson方法能正常工作,对象的结构却不正确。

我有一个名为'Tienda'的类,它有两个属性:(String) Name,(List<Venta>) ventas。其中'Venta'是另一个类,但当我调用toJson方法时,结果是一个对象'Tienda'的列表,其中属性List<Venta>为null。我应该做点什么不同的吗?

这是我获取List<Tienda>文件的方式:

Type listOfMyClassObject = new TypeToken<ArrayList<Tienda>>() {}.getType();
List<Tienda> tiendas = new GsonBuilder().setDateFormat("dd/MM/yyyy").create().fromJson(new FileReader("C:\\Archivos\\Ejemplo JSON Ventas.JSON"), listOfMyClassObject);

当我查看结果的内容时,是这样的:

Gson -> 导入包含对象的 JSON

英文:

I'm trying to use Gson to import an Json file with an Structure of classes I created. But, although the method toJson works, the structure of objects not.

I have a class called 'Tienda' which have two attributes: (String) Name, (List&lt;Venta&gt;) ventas. Where 'Venta' is other class, but when I call the method toJson, the result is a List of objects 'Tienda' where the attribute List&lt;Venta&gt; is null. Should I do something different?

This is how I get the file to the List&lt;Tienda&gt;:

Type listOfMyClassObject = new TypeToken&lt;ArrayList&lt;Tienda&gt;&gt;() {}.getType();
        List&lt;Tienda&gt; tiendas= new GsonBuilder().setDateFormat(&quot;dd/MM/yyy&quot;).create().fromJson(new FileReader(&quot;C:\\Archivos\\Ejemplo JSON Ventas.JSON&quot;), listOfMyClassObject);

And when I see the content of the result it is like this:

Gson -> 导入包含对象的 JSON

答案1

得分: 1

以下是翻译好的内容:

我认为您在类中的类型字段错误。该字段必须是Date类型(java.util.Date、java.sql.Timestamp、java.sql.Date)

这是一个获取日期对象的示例任务

//简单对象
Tienda demo = new GsonBuilder().setDateFormat("dd/MM/yyyy").create().fromJson(json, Tienda.class);

//数组
List<Tienda> demo = new GsonBuilder().setDateFormat("dd/MM/yyyy").create().fromJson(json, type);

这是我类"Tienda"的示例

import java.util.Date;

public class Tienda {
  public Date ventas;
}

我写了一些更多的测试,也许它们会帮助您解决这个问题

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class FormatData {

  @Test
  public void testDeserializeArray() {
    //给定
    String json = "[{\"ventas\":\"08/04/2020\"}, {\"ventas\":\"08/03/2020\"}]";
    Type type = new TypeToken<ArrayList<Tienda>>() {}.getType();
    //当
    List<Tienda> demo = new GsonBuilder().setDateFormat("dd/MM/yyyy").create().fromJson(json, type);
    //那么
    assertEquals(2, demo.size());
    assertEquals(1586296800000L, demo.get(0).ventas.getTime());
    assertEquals(1583622000000L, demo.get(1).ventas.getTime());
  }

  @Test
  public void testSimpleDeserialize() {
    //给定
    String json = "{\"ventas\":\"08/04/2020\"}";
    //当
    Tienda demo = new GsonBuilder().setDateFormat("dd/MM/yyyy").create().fromJson(json, Tienda.class);
    //那么
    assertEquals(1586296800000L, demo.ventas.getTime());
  }


  @Test
  public void testSerialize() {
    //给定
    Tienda demo = new Tienda();
    demo.ventas = new Date(1586296800000L);
    //当
    String json = new GsonBuilder().setDateFormat("dd/MM/yyyy").create().toJson(demo);
    //那么
    assertEquals("{\"ventas\":\"08/04/2020\"}", json);
  }
}
英文:

I think you have the wrong type field in class. The field must be of type Date (java.util.Date, java.sql.Timestamp, java.sql.Date)

this is a sample task to get a date object

//simple Object
Tienda demo = new GsonBuilder().setDateFormat(&quot;dd/MM/yyy&quot;).create().fromJson(json, Tienda.class);
//array
List&lt;Tienda&gt; demo = new GsonBuilder().setDateFormat(&quot;dd/MM/yyy&quot;).create().fromJson(json, type);

this is my example of the class "Tienda"

import java.util.Date;
public class Tienda {
public Date ventas;
}

I've written some more tests maybe they'll help you to solve this problem more

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class FormatData {
@Test
public void testDeserializeArray() {
//given
String json = &quot;[{\&quot;ventas\&quot;:\&quot;08/04/2020\&quot;}, {\&quot;ventas\&quot;:\&quot;08/03/2020\&quot;}]&quot;;
Type type = new TypeToken&lt;ArrayList&lt;Tienda&gt;&gt;() {
}.getType();
//when
List&lt;Tienda&gt; demo = new GsonBuilder().setDateFormat(&quot;dd/MM/yyy&quot;).create().fromJson(json, type);
//then
assertEquals(2, demo.size());
assertEquals(1586296800000L, demo.get(0).ventas.getTime());
assertEquals(1583622000000L, demo.get(1).ventas.getTime());
}
@Test
public void testSimpleDeserialize() {
//given
String json = &quot;{\&quot;ventas\&quot;:\&quot;08/04/2020\&quot;}&quot;;
//when
Tienda demo = new GsonBuilder().setDateFormat(&quot;dd/MM/yyy&quot;).create().fromJson(json, Tienda.class);
//then
assertEquals(1586296800000L, demo.ventas.getTime());
}
@Test
public void testSerialize() {
//given
Tienda demo = new Tienda();
demo.ventas = new Date(1586296800000L);
//when
String json = new GsonBuilder().setDateFormat(&quot;dd/MM/yyy&quot;).create().toJson(demo);
//then
assertEquals(&quot;{\&quot;ventas\&quot;:\&quot;08/04/2020\&quot;}&quot;, json);
}
}

答案2

得分: 0

我发现错误是如此愚蠢。我有一个以大写字母开头的Tienda,它应该是小写字母。它不会返回错误,只是将数组保持为空,因为它找不到完全相同的名称。

英文:

I found that the error was so fool. I have Tienda with capital letter and it should be with lower case. It does't return an error and just let the array as null because it doesn't find the exactly name.

huangapple
  • 本文由 发表于 2020年4月8日 05:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定