我是靠谱客的博主 专一水池,最近开发中收集的这篇文章主要介绍com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 99, fieldName,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

转载自:https://blog.csdn.net/jadyer/article/details/24395015

完整版见https://jadyer.github.io/




先列出来正确的解析代码,接着再描述问题详情。


首先是用到的JavaBean


 
 
  1. package com.jadyer.model;
  2. public class User {
  3. private String name;
  4. private Book book;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public Book getBook() {
  12. return book;
  13. }
  14. public void setBook(Book book) {
  15. this.book = book;
  16. }
  17. }

 
 
  1. package com.jadyer.model;
  2. public class Book {
  3. private String name;
  4. // private List<Author> author = new ArrayList<Author>();
  5. private Author author;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. // public List<Author> getAuthor() {
  13. // return author;
  14. // }
  15. // public void setAuthor(List<Author> author) {
  16. // this.author = author;
  17. // }
  18. public Author getAuthor() {
  19. return author;
  20. }
  21. public void setAuthor(Author author) {
  22. this.author = author;
  23. }
  24. }

 
 
  1. package com.jadyer.model;
  2. public class Author {
  3. private String name;
  4. private int money;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getMoney() {
  12. return money;
  13. }
  14. public void setMoney(int money) {
  15. this.money = money;
  16. }
  17. }

下面是生成json和解析json的代码


 
 
  1. package com.jadyer.main;
  2. import java.io.IOException;
  3. import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
  4. import org.apache.commons.lang3.builder.ToStringStyle;
  5. import org.codehaus.jackson.JsonParseException;
  6. import org.codehaus.jackson.map.JsonMappingException;
  7. import org.codehaus.jackson.map.ObjectMapper;
  8. import com.alibaba.fastjson.JSON;
  9. import com.jadyer.model.User;
  10. public class JSONDemo {
  11. // /**
  12. // * 生成{"book":{"author":[{"money":500,"name":"南派三叔"},{"money":200,"name":"孔二狗"}],"name":"网络文学"},"name":"玄玉"}
  13. // */
  14. // private static String format(){
  15. // List<Author> authorList = new ArrayList<Author>();
  16. // Author author11 = new Author();
  17. // author11.setName("南派三叔");
  18. // author11.setMoney(500);
  19. // authorList.add(author11);
  20. // Author author22 = new Author();
  21. // author22.setName("孔二狗");
  22. // author22.setMoney(200);
  23. // authorList.add(author22);
  24. // Book book = new Book();
  25. // book.setName("网络文学");
  26. // book.setAuthor(authorList);
  27. // User user = new User();
  28. // user.setName("玄玉");
  29. // user.setBook(book);
  30. // return JSON.toJSONString(user);
  31. // }
  32. public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
  33. String jsonText = "{"book":{"author":{"money":500,"name":"南派三叔"},"name":"网络文学"},"name":"玄玉"}";
  34. //用到的jar为fastjson-1.1.36.jar
  35. User userFromFastJSON = JSON.parseObject(jsonText, User.class);
  36. System.out.println(ReflectionToStringBuilder.toString(userFromFastJSON.getBook().getAuthor(), ToStringStyle.MULTI_LINE_STYLE));
  37. System.out.println( "----------------------------------------------------------------------------------");
  38. //用到的jar为jackson-all-1.9.11.jar
  39. User userFromJackson = new ObjectMapper().readValue(jsonText, User.class);
  40. System.out.println(ReflectionToStringBuilder.toString(userFromJackson.getBook().getAuthor(), ToStringStyle.MULTI_LINE_STYLE));
  41. }
  42. }

接下来描述下遇到的问题



 
 
  1. //上面解析正常时用到的json串
  2. //"{"book":{"author":{"money":500,"name":"南派三叔"},"name":"网络文学"},"name":"玄玉"}";
  3. //我在用fastjson和jackson解析时都会报错时用的json串
  4. //"{"book":{"author":[{"money":500,"name":"南派三叔"}],"name":"网络文学"},"name":"玄玉"}";
  5. //它俩的区别在于:book中只有一个author对象,只是多加了[],但其仍为合法的json串
  6. //并且http://tool.oschina.net/codeformat/json均验证通过

但在解析时,FastJSON会报下面的异常


 
 
  1. Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, expect {, actual ], pos 39
  2. at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java: 212)
  3. at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java: 96)
  4. at Fastjson_ASM_User_1.deserialze(Unknown Source)
  5. at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java: 513)
  6. at com.alibaba.fastjson.JSON.parseObject(JSON.java: 244)
  7. at com.alibaba.fastjson.JSON.parseObject(JSON.java: 220)
  8. at com.alibaba.fastjson.JSON.parseObject(JSON.java: 179)
  9. at com.alibaba.fastjson.JSON.parseObject(JSON.java: 323)
  10. at com.jadyer.main.JSONDemoDetail.main(JSONDemoDetail.java: 18)

Jackson会报下面的异常


 
 
  1. Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.jadyer.model.Author out of START_ARRAY token
  2. at [Source: java.io.StringReader@ 509ced8e; line: 1, column: 10] (through reference chain: com.jadyer.model.User[ "book"]->com.jadyer.model.Book[ "author"])
  3. at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java: 163)
  4. at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java: 219)
  5. at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java: 212)
  6. at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java: 875)
  7. at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java: 597)
  8. at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java: 299)
  9. at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java: 414)
  10. at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java: 697)
  11. at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java: 580)
  12. at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java: 299)
  13. at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java: 414)
  14. at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java: 697)
  15. at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java: 580)
  16. at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java: 2732)
  17. at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java: 1863)
  18. at com.jadyer.main.JSONDemoDetail.main(JSONDemoDetail.java: 22)

个人感觉Jackson的异常看上去更直观,可能是二者内部的验证逻辑不同导致的

不过还是倾向于FastJSON,即便一时之间没看明白它的异常描述,但那也只是这方面的经验不足而已

下面说一下解决办法:Book.java中将author修改为List<Author>类型的属性,并添加对应的setter和getter即可

个人的理解是:FastJSON和Jackson解析json为JavaBean时,先查看实体类的对应属性

当发现该属性不能承载多个对象值,且json串中恰好该属性使用的是中括号,这并不是它预期的大括号,于是报错了

最后

以上就是专一水池为你收集整理的com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 99, fieldName的全部内容,希望文章能够帮你解决com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 99, fieldName所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部