[英]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);
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);
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对象或使用实际数据进行更新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.