Fastjson: BeanToArray转换错误

Created on 29 Dec 2017  ·  3Comments  ·  Source: alibaba/fastjson

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)

question

Most helpful comment

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

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

All 3 comments

依赖最新fastjson版本

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

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

字段中有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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  4Comments

luckydzp picture luckydzp  ·  4Comments

wizardleeen picture wizardleeen  ·  4Comments

Ccixyj picture Ccixyj  ·  4Comments

lilijreey picture lilijreey  ·  4Comments