FastJson学习过程中出现的错误
初学者入门学习,多多指正!
错误展示
D:\\JDK_IDEA\\jdk\\bin\\java.exe...[{\"id1\":12,\"yourName1\":\"xiaoxiao\",\"birthday1\":\"2020-08-06\"},{\"id1\":13,\"yourName1\":\"laolao\",\"birthday1\":\"2020-08-06\"}]Exception in thread \"main\" com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0, fastjson-version 1.2.62at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:505)at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:288)at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:685)at com.alibaba.fastjson.JSON.parseObject(JSON.java:383)at com.alibaba.fastjson.JSON.parseObject(JSON.java:287)at com.alibaba.fastjson.JSON.parseObject(JSON.java:560)at com.tjson.test.JsonTest1.main(JsonTest1.java:24)Process finished with exit code 1
自己构建的Person实体类
public class Person {@JSONField(name = \"id1\",ordinal = 1)private int id;@JSONField(name = \"yourName1\",ordinal = 2)private String yourName;@JSONField(name = \"birthday1\",format = \"yyyy-MM-dd\",ordinal = 3)private Date birthday;public Person() {}public Person(int id, String yourName, Date birthday) {this.id = id;this.yourName = yourName;this.birthday = birthday;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getYourName() {return yourName;}public void setYourName(String yourName) {this.yourName = yourName;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return \"Person{\" +\"id=\" + id +\", yourName=\'\" + yourName + \'\\\'\' +\", birthday=\" + birthday +\'}\';}}
测试
public class JsonTest1 {public static void main(String[] args) {Person person1 = new Person(12,\"xiaoxiao\",new Date());Person person2 = new Person(13,\"laolao\",new Date());List<Person> list = new ArrayList<>();list.add(person1);list.add(person2);String s = JSON.toJSONString(list);System.out.println(s);Person person = JSON.parseObject(s, Person.class);System.out.println(person.toString());// List<Person> list1 = JSON.parseArray(s, Person.class);// System.out.println(list1);}}
错误发现:首先我是先创建了两个Person对象,并对其属性进行了初始化,然后将两个对象塞到list集合中,最后使用JSON.toJSONString(Object obj)将list转换为json字符串。如果我们仔细观察输出来的结果
[{\"id1\":12,\"yourName1\":\"xiaoxiao\",\"birthday1\":\"2020-08-06\"},{\"id1\":13,\"yourName1\":\"laolao\",\"birthday1\":\"2020-08-06\"}]
就会发现,外面是符号[ ],代表数组,里面是{ },代表对象,所以说我们反序列出来的结果是数组类型的。但是上面我使用的是
Person person = JSON.parseObject(s, Person.class);
这也就是说,我输出来的结果是Person类的对象,这结果显然与我们上面所分析的相矛盾。正确的写法应该是
List<Person> list1 = JSON.parseArray(s, Person.class);
正确结果展示:
[{\"id1\":12,\"yourName1\":\"xiaoxiao\",\"birthday1\":\"2020-08-06\"},{\"id1\":13,\"yourName1\":\"laolao\",\"birthday1\":\"2020-08-06\"}][Person{id=12, yourName=\'xiaoxiao\', birthday=Thu Aug 06 00:00:00 CST 2020}, Person{id=13, yourName=\'laolao\', birthday=Thu Aug 06 00:00:00 CST 2020}]
我们可以尝试着再对比一下这两个结果又什么不同:
首先是key不相同,例如上面结果的key–>“id1” 下面结果的key–>“id”
其二是日期格式不相同,上面是我们格式化后的日期,下面是最原始的日期格式。