fastjson BeanToArray转换错误

7xllpg7q  于 2021-11-27  发布在  Java
关注(0)|答案(3)|浏览(270)
public class BookDO {

    private Long bookId;

    private String bookName;

    private String authorName;

    private Integer wordCount;

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public Integer getWordCount() {
        return wordCount;
    }

    public void setWordCount(Integer wordCount) {
        this.wordCount = wordCount;
    }

}

String line = "[2, \"浪漫奇侠\", \"雨天不打伞\", 4536]";
BookDO book = JSON.parseObject(line, BookDO.class, Feature.SupportArrayToBean);

解析报错:
com.alibaba.fastjson.JSONException: not close json text, token : string
at com.alibaba.fastjson.parser.DefaultJSONParser.close(DefaultJSONParser.java:1475)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:369)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)

rmbxnbpk

rmbxnbpk1#

依赖最新fastjson版本

zbsbpyhn

zbsbpyhn2#

@JSONType(orders = {"bookId", "bookName", "authorName", "wordCount"})
    public static class BookDO {
    }

缺省parse的顺序是按字母序,需要其他顺序,通过@JSONType指定

wlzqhblo

wlzqhblo3#

字段中有tab如何处理?

String line = "[2, \"浪漫奇侠\\t\", \"雨天不打伞\", 4536]";
BookDO book = JSON.parseObject(line, BookDO.class, Feature.SupportArrayToBean);

Exception in thread "main" com.alibaba.fastjson.JSONException: 14
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:658)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)

相关问题