fastjson 1.2.24 parseArray会把 "[]" 整体当成List< T>的一个项

wxclj1h5  于 3个月前  发布在  其他
关注(0)|答案(7)|浏览(65)

将包含字符串
"pictureList":"["http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000","http://common.cnblogs.com/images/icon_weibo_24.png"]"
的json使用JSON.parseArray(json, User.class) 到 包含代码
public class User implements Serializable {
private List pictureList;
public List getPictureList() {
return pictureList;
}
public User setPictureList(List pictureList) {
this.pictureList = pictureList;
return this;
}
}
的User.java后,pictureList
应该包含以下两项:
" http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000 "
" http://common.cnblogs.com/images/icon_weibo_24.png "

实际上却只含一项:
"["http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000","http://common.cnblogs.com/images/icon_weibo_24.png"]"

建议解决方法:
当要parse的对象为Collection时,先去掉对应字符串前后的双引号"。

2exbekwf

2exbekwf1#

1.2.21也有这个问题

jaxagkaj

jaxagkaj2#

public void test_for_issue() throws Exception {
    String json = "{\"pictureList\":[\"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000\",\"http://common.cnblogs.com/images/icon_weibo_24.png\"]}";

    User user = JSON.parseObject(json, User.class);
    assertNotNull(user.pictureList);
    assertEquals(2, user.pictureList.size());
    assertEquals("http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000", user.pictureList.get(0));
    assertEquals("http://common.cnblogs.com/images/icon_weibo_24.png", user.pictureList.get(1));
}

public static class User implements Serializable {
    private List<String> pictureList;
    public List<String> getPictureList() {
        return pictureList;
    }
    public User setPictureList(List<String> pictureList) {
        this.pictureList = pictureList;
        return this;
    }
}

我测试到是好的,但可能和你要求不符合

wwodge7n

wwodge7n3#

问题就在pictureList这个JSONArray外面加了双引号,"["http://static.oschina...]",MySQL这边不管用VarChar还是JSON存储,取出来都会自动加上双引号,请问有没有办法去除(不影响正常的字符串)?如果能在MySQL这边解决更好。

aelbi1ox

aelbi1ox5#

获取单个User用parseObject也是这样

4ktjp1zp

4ktjp1zp6#

这应该是不符合json标准吧,[]外面再加双引号,随便一个json校验工具都通不过

gmxoilav

gmxoilav7#

@googleweb 会通过,只是会当成字符串而不是数组😂

相关问题