com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 917

lqfhib0f  于 2021-11-27  发布在  Java
关注(0)|答案(3)|浏览(1384)

我发现这个出错是因为json里有一个songList,一个songlist 这样两个key,我的实体类里只写了songList(songlist这个字段不需要)。

1.2.32 处理json songlist这个key时,匹配到是类型是songList
1.2.36 在抛出这个异常之前有一个空指针

cpjpxq1n

cpjpxq1n1#

能提供异常错误堆栈信息么?

smdnsysy

smdnsysy2#

1.2.32
Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 917 at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:370) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1070) at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_2_DigitalSongPlatform.deserialze(Unknown Source) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:211) at com.alibaba.fastjson.parser.deserializer.ArrayListTypeFieldDeserializer.parseArray(ArrayListTypeFieldDeserializer.java:178) at com.alibaba.fastjson.parser.deserializer.ArrayListTypeFieldDeserializer.parseField(ArrayListTypeFieldDeserializer.java:65) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:888) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:633) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1070) at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_DigitalAlbumPlatform.deserialze(Unknown Source) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:211) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:726) at com.alibaba.fastjson.serializer.CollectionCodec.deserialze(CollectionCodec.java:120) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:642) at com.alibaba.fastjson.JSON.parseObject(JSON.java:354) at com.alibaba.fastjson.JSON.parseObject(JSON.java:322) at com.alibaba.fastjson.JSON.parseObject(JSON.java:236) at com.XXX.service.XXX.XXX(XXX.java:97)

1.2.36
Exception in thread "main" com.alibaba.fastjson.JSONException at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:627) at com.alibaba.fastjson.JSON.parseObject(JSON.java:361) at com.alibaba.fastjson.JSON.parseObject(JSON.java:329) at com.alibaba.fastjson.JSON.parseObject(JSON.java:243) at com.XXX.service.XXX.XXX(XXX.java:97) Caused by: java.lang.NullPointerException at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:413) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1146) at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_2_DigitalSongPlatform.deserialze(Unknown Source) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:268) at com.alibaba.fastjson.parser.deserializer.ArrayListTypeFieldDeserializer.parseArray(ArrayListTypeFieldDeserializer.java:178) at com.alibaba.fastjson.parser.deserializer.ArrayListTypeFieldDeserializer.parseField(ArrayListTypeFieldDeserializer.java:65) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:973) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:718) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1146) at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_DigitalAlbumPlatform.deserialze(Unknown Source) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:268) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:709) at com.alibaba.fastjson.serializer.CollectionCodec.deserialze(CollectionCodec.java:120) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:623) ... 5 more

nuypyhwy

nuypyhwy3#

1.2.32
代码是这个样子的 如果注解掉songlist属性,会报错,如果不注解 songList属性为null

public static void main(String[] args) {

        String json = "[{\"song_list\":[{\"val\":1,\"v_al\":2},{\"val\":2,\"v_al\":2},{\"val\":3,\"v_al\":2}],\"songlist\":\"v_al\"}]";
        List<Value> parseObject = JSON.parseObject(json, new TypeReference<List<Value>>() {
        });
        for (Value value : parseObject) {
            System.out.println(value.songList + "  " );
        }

    }

    public static class Value {
        @JSONField(alternateNames = {"song_list", "songList"})
        List<Value2> songList;

        @JSONField(alternateNames = {"songlist"})
        String songlist;

        public List<Value2> getSongList() {
            return songList;
        }

        public void setSongList(List<Value2> songList) {
            this.songList = songList;
        }

            public String getSonglist() {
                return songlist;
            }

            public void setSonglist(String songlist) {
                this.songlist = songlist;
            }
    }

    public static class Value2 {
        int val;

        int v_al;

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public int getV_al() {
            return v_al;
        }

        public void setV_al(int v_al) {
            this.v_al = v_al;
        }

        @Override
        public String toString() {
            return "Value2{" +
                    "val=" + val +
                    ", v_al=" + v_al +
                    '}';
        }
    }

相关问题