简体   繁体   English

Gson重建反序列化对象时丢失数据

[英]Gson losing data when reconstructing deserializing object

I don't really know how to word the title of this question, so if anyone has a better title please suggest it. 我真的不知道该问题标题的措辞如何,因此,如果有人有更好的标题,请提出建议。 I am trying to encode/decode an ArrayList of Sessions s and each session has an ArrayList of Shot s. 我正在尝试对Sessions的ArrayList进行编码/解码,并且每个会话都有Shot的ArrayList。 The problem I am having is that when I try to encode the list of Sessions, Gson doesn't dig into the array of Shots for each session and encode the ArrayList of shots, but just leaves an empty array: 我遇到的问题是,当我尝试对Sessions列表进行编码时,Gson不会为每个会话挖掘Shots数组并对其进行编码,而只留下一个空数组:

[  
   {  
      "ANGLE":71.356865,
      "DATE":"2015-11-25-11-02",
      "KEY":62,
      "SHOTS":[  

      ]
   },
   {  
      "ANGLE":75.26292,
      "DATE":"2015-11-25-11-02",
      "KEY":63,
      "SHOTS":[  

      ]
   }
]

Sessions Class 课时班

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Session {
    @SerializedName("SHOTS") @Expose
    public ArrayList<Shot> shots;
    @SerializedName("SCORE") @Expose
    public float averageScore;
    @SerializedName("DATE") @Expose
    public String date;
    @SerializedName("KEY") @Expose
    public int pk;
    ...
}

Shots class 铅球课

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Shot {
    public String identifier;
    @SerializedName("SCORE") @Expose
    public double score;
    @SerializedName("TYPE") @Expose
    public int problem;
    @SerializedName("PROBLEM_STRING") @Expose
    public String problemString;
    ...
}

Building the json string: 构建json字符串:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String json = gson.toJson(sessions);

update 更新

After looking into this more, data is being lossed when I am rebuilding the object and not when I am serializing it. 在深入研究之后,当我重建对象而不是序列化对象时,数据丢失了。 This is the code I am using to deserialize it: 这是我用来反序列化的代码:

Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Session>>(){}.getType();
sessions = gson.fromJson(sessionsString, type);

update 更新

After even more digging, my data loss is not related to the encoding/deconging of Gson . 经过更多挖掘之后, 我的数据丢失与Gson的编码/衰减无关 I would like to apologize to Google for blaming them. 我想向Google道歉。 My problem seems to be related to AsyncTask and I have reposted it here . 我的问题似乎与AsyncTask有关,我在这里重新发布了它。

First are you sure of the name ? 首先您确定名字吗? Second the only array I see is the score 其次,我看到的唯一数组是分数

 "SCORE":[  

  ]

please recheck the json object or update it with actual data 请重新检查json对象或使用实际数据进行更新

错误的反序列化列表<object>与 gson<div id="text_translate"><p> 我用List&lt;Object&gt;调试CUSTOMER object:</p><p> <a href="https://i.stack.imgur.com/Gh2Mx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gh2Mx.png" alt="在此处输入图像描述"></a></p><p> 这是 CUSTOMER.class:</p><pre> public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name = "CITY", type = CITY.class), @XmlElement(name = "COUNTRY", type = COUNTRY.class), }) protected List&lt;Object&gt; addressAndZIPAndCITY; // other fields }</pre><p> 但是当我反序列化并从中创建 json 时,它仅包含:</p><pre> { "addressAndZIPAndCITY": [ { "value": "some value", "type": "some type" }, { "value": "some value 2", "type": "some type 2" }] }</pre><p> 缺少 ADRESS、ZIP、CITY 和 COUNTRY 对象标题。 所以反序列化不好。</p><p> 我无法更改List&lt;Object&gt;声明。 是否可以选择将其反序列化为带有 ADRESS、ZIP、CITY 等的 json? 像这样:</p><pre> { "addressAndZIPAndCITY": [{ "ADDRESS": { "value": "some value", "type": "some type" } }, { "ZIP": { "value": "some value 2", "type": "some type 2" } } ] }</pre><p> 编辑:我的意思是它看起来像 GSON 不知道哪个 object 在那里,但可能你知道如何向 model 添加一些注释或其他东西来识别它?</p></div></object> - Wrong deserializing List<Object> with gson

暂无
暂无

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

相关问题 Gson 反序列化对象时忽略 null - Gson ignore null when deserializing object Gson不反序列化JSON数据 - Gson not deserializing JSON data 反序列化地图 <Object, Object> 与GSon - Deserializing Map<Object, Object> with GSon 当预先知道密钥时,使用gson反序列化JSON对象 - Deserializing a JSON object using gson when keys are now known in advance 错误的反序列化列表<object>与 gson<div id="text_translate"><p> 我用List&lt;Object&gt;调试CUSTOMER object:</p><p> <a href="https://i.stack.imgur.com/Gh2Mx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gh2Mx.png" alt="在此处输入图像描述"></a></p><p> 这是 CUSTOMER.class:</p><pre> public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name = "CITY", type = CITY.class), @XmlElement(name = "COUNTRY", type = COUNTRY.class), }) protected List&lt;Object&gt; addressAndZIPAndCITY; // other fields }</pre><p> 但是当我反序列化并从中创建 json 时,它仅包含:</p><pre> { "addressAndZIPAndCITY": [ { "value": "some value", "type": "some type" }, { "value": "some value 2", "type": "some type 2" }] }</pre><p> 缺少 ADRESS、ZIP、CITY 和 COUNTRY 对象标题。 所以反序列化不好。</p><p> 我无法更改List&lt;Object&gt;声明。 是否可以选择将其反序列化为带有 ADRESS、ZIP、CITY 等的 json? 像这样:</p><pre> { "addressAndZIPAndCITY": [{ "ADDRESS": { "value": "some value", "type": "some type" } }, { "ZIP": { "value": "some value 2", "type": "some type 2" } } ] }</pre><p> 编辑:我的意思是它看起来像 GSON 不知道哪个 object 在那里,但可能你知道如何向 model 添加一些注释或其他东西来识别它?</p></div></object> - Wrong deserializing List<Object> with gson 使用GSON和RETROFIT反序列化复杂的JSON对象 - Deserializing complex JSON object with GSON and RETROFIT 使用GSON反序列化包含JSON的对象 - Deserializing an object that contains JSON using GSON GSON JsonSyntaxException反序列化Java Date对象 - GSON JsonSyntaxException deserializing Java Date Object 使用 Gson 反序列化映射键需要一个对象 - Deserializing map key with Gson expects an object 使用GSON将JSON对象反序列化为字符串字段 - Deserializing JSON object to string field with GSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM