簡體   English   中英

Gson重建反序列化對象時丟失數據

[英]Gson losing data when reconstructing deserializing object

我真的不知道該問題標題的措辭如何,因此,如果有人有更好的標題,請提出建議。 我正在嘗試對Sessions的ArrayList進行編碼/解碼,並且每個會話都有Shot的ArrayList。 我遇到的問題是,當我嘗試對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":[  

      ]
   }
]

課時班

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;
    ...
}

鉛球課

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;
    ...
}

構建json字符串:

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

更新

在深入研究之后,當我重建對象而不是序列化對象時,數據丟失了。 這是我用來反序列化的代碼:

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

更新

經過更多挖掘之后, 我的數據丟失與Gson的編碼/衰減無關 我想向Google道歉。 我的問題似乎與AsyncTask有關,我在這里重新發布了它。

首先您確定名字嗎? 其次,我看到的唯一數組是分數

 "SCORE":[  

  ]

請重新檢查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不反序列化JSON數據 反序列化地圖 <Object, Object> 與GSon 當預先知道密鑰時,使用gson反序列化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> 使用GSON和RETROFIT反序列化復雜的JSON對象 使用GSON反序列化包含JSON的對象 GSON JsonSyntaxException反序列化Java Date對象 使用 Gson 反序列化映射鍵需要一個對象 使用GSON將JSON對象反序列化為字符串字段
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM