开发者社区 问答 正文

GSON深度解析报错?报错

就两个类:我用的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);

    }

}




展开
收起
爱吃鱼的程序员 2020-06-22 17:34:33 623 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
    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再一步一步往下取
    第一次在这里发问题,很快就得到了正确答案,非常感谢!!!!

    引用来自“aweiwang”的评论


    第一次在这里发问题,很快就得到了正确答案,非常感谢!!!!
    2020-06-22 17:34:49
    赞同 展开评论
问答分类:
问答地址: