就两个类:我用的GSON版本是(gson-2.2.4.jar)
错误为: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
at com.google.gson.Gson.fromJson(Gson.java:815)
............................
........................
代码如下:
第一个类Dept.java
package com.test.json;
import java.util.List;
public class Dept {
private String text;
private String id;
private boolean checked;
private boolean leaf;
private List<Dept> children;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean getChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public boolean getLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public List<Dept> getChildren() {
return children;
}
public void setChildren(List<Dept> children) {
this.children = children;
}
}
第二个类GsonTest.java
package com.test.json;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonTest {
/**
* @param args
*/
public static void main(String[] args) {
Dept d0 = new Dept();
d0.setId("01");
d0.setText("tecknolegedept");
Dept d01 = new Dept();
d01.setId("0101");
d01.setText("tecknolegedept0101");
d01.setLeaf(true);
d01.setChildren(Collections.EMPTY_LIST);
Dept d02 = new Dept();
d02.setId("0102");
d02.setText("tecknolegedept0102");
d02.setLeaf(true);
d02.setChildren(Collections.EMPTY_LIST);
List<Dept> set0 = new ArrayList<Dept>();
set0.add(d01);
set0.add(d02);
d0.setChildren(set0);
//--------------------------------
Dept d1 = new Dept();
d1.setId("02");
d1.setText("tecknolegedept");
Dept d11 = new Dept();
d11.setId("0201");
d11.setText("tecknolegedept0201");
d11.setLeaf(true);
d11.setChildren(Collections.EMPTY_LIST);
Dept d12 = new Dept();
d12.setId("0202");
d12.setText("tecknolegedept0202");
d12.setLeaf(true);
d12.setChildren(Collections.EMPTY_LIST);
List<Dept> set1 = new ArrayList<Dept>();
set1.add(d11);
set1.add(d12);
d1.setChildren(set1);
//--------------------------------
Dept root = new Dept();
root.setId("0");
root.setText("root");
List<Dept> r = new ArrayList<Dept>();
r.add(d0);
r.add(d1);
root.setChildren(r);
Gson gson = new Gson();
String gsonString = gson.toJson(root);
System.out.println(gsonString);
List<Dept> depts = gson.fromJson(gsonString, new TypeToken<List<Dept>>(){}.getType());
System.out.println(depts);
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
List<Dept>depts=gson.fromJson(gsonString,newTypeToken<List<Dept>>(){}.getType());这一句改成Deptdepts=gson.fromJson(gsonString,Dept.class);gsonString是一个对象的json而不是list,楼主先反向得到对象root再一步一步往下取
List<Dept>depts=gson.fromJson(gsonString,newTypeToken<List<Dept>>(){}.getType());这一句改成Deptdepts=gson.fromJson(gsonString,Dept.class);gsonString是一个对象的json而不是list,楼主先反向得到对象root再一步一步往下取