Java JSON反序列化,GSON,android,错误
我得到了自定义对象MyList的ArrayList。MyList是HashMap。MyList包含自定义对象MyListElement的ArrayList(它也是HashMap)。在GSON序列化之后,它看起来是这样的(为了更好的视觉效果,几乎不正确): 但是如果我尝试反序列化它,我会得到错误:Java JSON反序列化,GSON,android,错误,java,android,json,gson,deserialization,Java,Android,Json,Gson,Deserialization,我得到了自定义对象MyList的ArrayList。MyList是HashMap。MyList包含自定义对象MyListElement的ArrayList(它也是HashMap)。在GSON序列化之后,它看起来是这样的(为了更好的视觉效果,几乎不正确): 但是如果我尝试反序列化它,我会得到错误: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.llaerto.my
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.llaerto.mysecondbuylist.MyList.
我的反序列化代码:
public void gsonGetFromSerialize() {
String json = "";
try {
FileReader fileReader = new FileReader(this.getFilesDir().getAbsolutePath() + "/mydb.json");
BufferedReader reader = new BufferedReader(fileReader);
String line;
while ((line = reader.readLine()) != null) {
json = json + line + "\n";
}
Log.d("TAG", json);
fileReader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Type listType = new TypeToken<ArrayList<MyList>>() {}.getType();
list = new Gson().fromJson(json, listType);
}
public void gsonGetFromSerialize(){
字符串json=“”;
试一试{
FileReader FileReader=newFileReader(this.getFilesDir().getAbsolutePath()+“/mydb.json”);
BufferedReader reader=新的BufferedReader(文件读取器);
弦线;
而((line=reader.readLine())!=null){
json=json+line+“\n”;
}
Log.d(“TAG”,json);
fileReader.close();
reader.close();
}捕获(IOE异常){
e、 printStackTrace();
}
类型listType=newTypeToken(){}.getType();
list=new Gson().fromJson(json,listType);
}
对不起,我的语言,还有一些愚蠢的问题。谢谢你的帮助
MyList类别代码:
public class MyList extends HashMap<String, Object> implements Serializable{
public static final String LIST_NAME = "list name";
public static final String LIST_LIST_MAIN = "list main list";
public static final String LIST_LIST_BUYED = "list buyed list";
public static final String LIST_QUANTITY = "list quantity";
public MyList(String name) {
this.put(LIST_NAME, name);
this.put(LIST_LIST_MAIN, new ArrayList<MyListElement>());
this.put(LIST_LIST_BUYED, new ArrayList<MyListElement>());
this.put(LIST_QUANTITY, "Не купленных: " + 0);
}
@Override
public String toString() {
return (String) this.get(LIST_NAME);
}
public void getSum() {
int i = ((ArrayList) this.get(LIST_LIST_MAIN)).size();
this.put(LIST_QUANTITY, "Не купленных: " + i);
}
public String getName() {
return this.get(LIST_NAME).toString();
}
public void setName(String name) {
this.put(LIST_NAME, name);
}
public void toListAdd(MyListElement element) {
ArrayList<MyListElement> list;
list = (ArrayList<MyListElement>) this.get(LIST_LIST_MAIN);
list.add(element);
this.put(LIST_LIST_MAIN, list);
}
}
public class MyListElement extends HashMap<String, Object> implements Serializable {
public static final String LIST_ELEMENT_NAME = "listelement name";
public static final String LIST_ELEMENT_QUANTITY = "listelement quantity";
public static final String LIST_ELEMENT_COLOR = "listelement color";
public static final String LIST_ELEMENT_SUM = "listelement sum";
public MyListElement(String name, float quantity, float sum) {
this.put(LIST_ELEMENT_NAME, name);
this.put(LIST_ELEMENT_QUANTITY, quantity);
this.put(LIST_ELEMENT_SUM, sum);
//this.put(LIST_ELEMENT_COLOR, color);
}
}
公共类MyList扩展HashMap实现可序列化{
公共静态最终字符串列表\u NAME=“列表名称”;
公共静态最终字符串列表\u LIST\u MAIN=“列表主列表”;
公共静态最终字符串LIST\u LIST\u BUYED=“LIST BUYED LIST”;
公共静态最终字符串列表\u QUANTITY=“列表数量”;
公共MyList(字符串名称){
此.put(列表名称、名称);
this.put(LIST_LIST_MAIN,new ArrayList());
this.put(LIST\u LIST\u BUYED,new ArrayList());
本表所列数量为+0;
}
@凌驾
公共字符串toString(){
返回(字符串)this.get(列表\名称);
}
公共无效getSum(){
inti=((ArrayList)this.get(LIST_LIST_MAIN)).size();
本表(列出数量“i”);
}
公共字符串getName(){
返回此.get(LIST_NAME).toString();
}
公共void集合名(字符串名){
此.put(列表名称、名称);
}
public void toListAdd(MyListElement元素){
数组列表;
list=(ArrayList)this.get(list\u list\u MAIN);
列表。添加(元素);
this.put(LIST\u LIST\u MAIN,LIST);
}
}
MyListElement类代码:
public class MyList extends HashMap<String, Object> implements Serializable{
public static final String LIST_NAME = "list name";
public static final String LIST_LIST_MAIN = "list main list";
public static final String LIST_LIST_BUYED = "list buyed list";
public static final String LIST_QUANTITY = "list quantity";
public MyList(String name) {
this.put(LIST_NAME, name);
this.put(LIST_LIST_MAIN, new ArrayList<MyListElement>());
this.put(LIST_LIST_BUYED, new ArrayList<MyListElement>());
this.put(LIST_QUANTITY, "Не купленных: " + 0);
}
@Override
public String toString() {
return (String) this.get(LIST_NAME);
}
public void getSum() {
int i = ((ArrayList) this.get(LIST_LIST_MAIN)).size();
this.put(LIST_QUANTITY, "Не купленных: " + i);
}
public String getName() {
return this.get(LIST_NAME).toString();
}
public void setName(String name) {
this.put(LIST_NAME, name);
}
public void toListAdd(MyListElement element) {
ArrayList<MyListElement> list;
list = (ArrayList<MyListElement>) this.get(LIST_LIST_MAIN);
list.add(element);
this.put(LIST_LIST_MAIN, list);
}
}
public class MyListElement extends HashMap<String, Object> implements Serializable {
public static final String LIST_ELEMENT_NAME = "listelement name";
public static final String LIST_ELEMENT_QUANTITY = "listelement quantity";
public static final String LIST_ELEMENT_COLOR = "listelement color";
public static final String LIST_ELEMENT_SUM = "listelement sum";
public MyListElement(String name, float quantity, float sum) {
this.put(LIST_ELEMENT_NAME, name);
this.put(LIST_ELEMENT_QUANTITY, quantity);
this.put(LIST_ELEMENT_SUM, sum);
//this.put(LIST_ELEMENT_COLOR, color);
}
}
公共类MyListElement扩展HashMap实现可序列化{
公共静态最终字符串列表\u元素\u NAME=“listelement名称”;
公共静态最终字符串列表\u元素\u数量=“列表元素数量”;
公共静态最终字符串列表\u元素\u COLOR=“liselement COLOR”;
公共静态最终字符串列表\u元素\u SUM=“liselement SUM”;
公共MyListElement(字符串名称、浮动数量、浮动总和){
this.put(列表元素名称、名称);
此.put(列表元素数量、数量);
这个.put(LIST\u ELEMENT\u SUM,SUM);
//此.put(列表元素颜色、颜色);
}
}
很抱歉,我在第一次发布时没有看到它。我看不到使用Map表示json有任何好处,也不明白为什么要这样使用它。依我看,使用类会更好地理解。所以请把这个答案作为一个建议。我希望将来有人会使用它 首先,您需要创建根类。让我们称之为
RootItem
。您的Json(实际上看起来)是RootItem
的数组
public class RootItem {
@SerializedName("list buyed list")
private Collection<String> listBuyedList;
@SerializedName("list name")
private String listName;
@SerializedName("list quantity")
private String listQuality;
@SerializedName("list main list")
private Collection<MainListItem> listMainList;
// Getters & Setters
}
import com.google.gson.annotations.SerializedName;
public class MainListItem {
@SerializedName("listelement quantity")
private Integer listElementQuantity;
@SerializedName("listelement sum")
private Integer listElementSum;
@SerializedName("listelement name")
private String listElementName;
// Getters & Setters
}
最后,您需要将JSON文件解析为集合
,因为它是根项
对象的数组
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.util.Collection;
public class GsonApp {
private static final String TEST_JSON = "[\n" +
" {\n" +
" \"list buyed list\": [],\n" +
" \"list name\": \"item1\",\n" +
" \"list quantity\": \"Не купленных: 0\",\n" +
" \"list main list\": [\n" +
" {\n" +
" \"listelement quantity\": 4,\n" +
" \"listelement sum\": 1,\n" +
" \"listelement name\": \"item 1\"\n" +
" },\n" +
" {\n" +
" \"listelement quantity\": 2,\n" +
" \"listelement sum\": 2,\n" +
" \"listelement name\": \"item 2\"\n" +
" },\n" +
" {\n" +
" \"listelement quantity\": 4,\n" +
" \"listelement sum\": 3,\n" +
" \"listelement name\": \"item 3\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"list buyed list\": [],\n" +
" \"list name\": \"item2\",\n" +
" \"list quantity\": \"Не купленных: 0\",\n" +
" \"list main list\": [\n" +
" {\n" +
" \"listelement quantity\": 5,\n" +
" \"listelement sum\": 1,\n" +
" \"listelement name\": \"item 4\"\n" +
" },\n" +
" {\n" +
" \"listelement quantity\": 4,\n" +
" \"listelement sum\": 7,\n" +
" \"listelement name\": \"item 5\"\n" +
" },\n" +
" {\n" +
" \"listelement quantity\": 6,\n" +
" \"listelement sum\": 3,\n" +
" \"listelement name\": \"item 6\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"]";
public static void main(String[] args) throws Exception {
final Gson gson = new GsonBuilder().create();
final Collection<RootItem> rootItems = gson.fromJson(TEST_JSON, new TypeToken<Collection<RootItem>>() {
}.getType());
System.out.println(rootItems);
}
}
但您的json仍然是一个集合,因此我们需要使用集合将数据获取到地图中
public static void main(String[] args) throws Exception {
final Gson gson = new GsonBuilder().create();
final Collection<RootClass> items = gson.fromJson(TEST_JSON, new TypeToken<Collection<RootClass>>() {
}.getType());
System.out.println(items);
}
publicstaticvoidmain(字符串[]args)引发异常{
final Gson Gson=new GsonBuilder().create();
final Collection items=gson.fromJson(TEST_JSON,new TypeToken()){
}.getType());
系统输出打印项次(项);
}
您也可以共享MyList类的代码吗!您的json文件仍然有错误。您始终可以使用在线工具验证json文件。谢谢你的回答。我需要HashMap,因为我在ListView(android)中使用SimpleDapter:(@llaerto您需要哪一个作为映射?我在主ListView中使用SimpleDapter,每个都使用列表名称和数量。当我打开此MyList时,将使用更多的2个SimpleDapter-1个用于主列表,1个用于购买列表(两个列表均由MyListElement项组成).这是我的第一个应用程序,我只是学习,可能我现在正在做一个非常糟糕的应用程序:(@llaerto我不擅长Android。据我所知,您可以编写自己的适配器。例如,您可以使用SimpleAdapter
为RootItem
和MainListItem
编写自己的适配器。而不是在视图中使用它们。@llaerto您可能希望使用ArrayAdapter而不是SimpleAdapter——这更典型ical用于列表视图,不需要地图。请参阅,也可以