Gson解析json时出现Expected a string but was BEGIN_ARRAY异常,由此发现Gson和FastJson区别

用Gson解析一段json,想把一个属性定义成String,来接收所有的数据类型,拿到解析出来的String再做处理,结果遇到这个异常. 

这个异常大家应该都遇到过,就是我们定义的对象的某个属性是string类型的,但json中确是Array类型的.

 这是因为:Gson中,关键字后面出现""引起来的内容将会被只认为是STRING,“{}”只被认为是类,“[]”只被认为是List,这个几乎是强制性的。

 就是说如果你的实体预计是获取String的变量,但是关键字后面对应的却出现了“{”或“[”,那么这个转换将被认为是错误的,抛出异常。

想到以前用的FastJson好像是不存在这种问题的,于是做了测试.

@Test
    public void jsonTest(){
        // json文件
        File file=new File("C://data.json");
        // 文件转String
        String data=readToString(file.getAbsolutePath());

        // fastjson解析
        useFastJson(data);
        // Gson解析
        useGson(data);
    }

    /**
     * 用fastjson解析
     * @param data
     */
    private void useFastJson(String data){
        Article article= JSON.parseObject(data, Article.class);
        System.out.println(article.getDetail());
    }

    /**
     * 用Gosn 解析
     * @param data
     */
    private void useGson(String data){
        Article article= new Gson().fromJson(data,Article.class);
        System.out.println(article.getDetail());
    }

/**
     * 文件中获取String
     * @param fileName
     * @return
     */
    public static String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }
结果:
[{"catalog":"环境搭建","article":[{"read":1,"column":9,"id":2,"title":"IntelliJIDEA环境搭建","slug":"static/kotlin_article/0_1.html"},{"read":1,"column":9,"id":3,"title":"Eclipse环境搭建","slug":"static/kotlin_article/0_2.html"},{"read":1,"column":9,"id":4,"title":"使用命令行编译","slug":"static/kotlin_article/0_3.html"},{"read":1,"column":9,"id":5,"title":"Android环境搭建","slug":"static/kotlin_article/0_4.html"}]}]

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 2 column 16 path $.detailcom.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 2 column 16 path $.detailat com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)at com.google.gson.Gson.fromJson(Gson.java:888)at com.google.gson.Gson.fromJson(Gson.java:853)at com.google.gson.Gson.fromJson(Gson.java:802)at com.google.gson.Gson.fromJson(Gson.java:774)at per.wsj.myapplication.ExampleUnitTest.useGson(ExampleUnitTest.java:60)at per.wsj.myapplication.ExampleUnitTest.jsonTest(ExampleUnitTest.java:43)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)

果然Fastjson是可以的,好坑啊 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值