当前位置:   article > 正文

Gson的使用以及com.google.gson.JsonSyntaxException的解决方法

com.google.gson.jsonsyntaxex

一、Gson的使用:

1.实体类

  1. package com.zsl.test;
  2. /**
  3. * 实体类
  4. * @author zsl
  5. * @version 2015年1月3日 19:10:10
  6. *
  7. */
  8. public class Person {
  9. private int id;
  10. private String name;
  11. private String address;
  12. private String email;
  13. private double money;
  14. @Override
  15. public String toString() {
  16. // TODO Auto-generated method stub
  17. return "编号:"+id+" 名字:"+name+" 地址:"+address+" E-mail:"+email+" 钱:"+money;
  18. }
  19. public Person() {
  20. // TODO Auto-generated constructor stub
  21. }
  22. public Person(int id, String name, String address, String email,
  23. double money) {
  24. super();
  25. this.id = id;
  26. this.name = name;
  27. this.address = address;
  28. this.email = email;
  29. this.money = money;
  30. }
  31. public int getId() {
  32. return id;
  33. }
  34. public void setId(int id) {
  35. this.id = id;
  36. }
  37. public String getName() {
  38. return name;
  39. }
  40. public void setName(String name) {
  41. this.name = name;
  42. }
  43. public String getAddress() {
  44. return address;
  45. }
  46. public void setAddress(String address) {
  47. this.address = address;
  48. }
  49. public String getEmail() {
  50. return email;
  51. }
  52. public void setEmail(String email) {
  53. this.email = email;
  54. }
  55. public double getMoney() {
  56. return money;
  57. }
  58. public void setMoney(double money) {
  59. this.money = money;
  60. }
  61. }

2.json转换为实体对象

  1. /**
  2. * 转换一个Java Bean的时候
  3. * 记住不能使用[],只有在使用集合的时候才可以使用
  4. * 如上图贴的那个异常就是由此原因造成的,另外
  5. * 格式要规范,中间不能出现空格字符
  6. *
  7. **/
  8. Gson g=new Gson();
  9. String jsonBean="<span style="color:#ff0000;">{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}</span>";
  10. Person p=g.fromJson(jsonBean, Person.class);
  11. System.out.println(p);

结果:

编号:0  名字:散仙0   地址:北京市0   E-mail:54152541@qq.com  钱:500.0


3.json转换为List<Person

<pre name="code" class="java"><span style="white-space:pre">	</span>Gson g=new Gson();
String jsonBean1=" [{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0},{\"id\":1,\"name\":\"散仙0\ ",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}]"; List<Person> lists=g.fromJson(jsonBean1,new TypeToken<List<Person>>(){}.getType()); for(Person pp:lists){ System.out.println(pp); }

 
j结果:

  1. 编号:0 名字:散仙0 地址:北京市0 E-mail:54152541@qq.com 钱:500.0
  2. 编号:1 名字:散仙0 地址:北京市0 E-mail:54152541@qq.com 钱:500.0

二、com.google.gson.JsonSyntaxException的解决方法

1.如果我现在将【一、2】中的

String jsonBean="{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}";
改为:(加上[ ])

String jsonBean="<span style="color:#ff0000;">[</span>{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}<span style="color:#ff0000;">]</span>";

结果:

  1. Exception in thread "main" com.google.gson.JsonSyntaxException: <span style="color:#ff0000;">java.lang.IllegalStateException</span>: <span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;">Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path</span></span> $
  2. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:187)
  3. at com.google.gson.Gson.fromJson(Gson.java:805)
  4. at com.google.gson.Gson.fromJson(Gson.java:770)
  5. at com.google.gson.Gson.fromJson(Gson.java:719)
  6. at com.google.gson.Gson.fromJson(Gson.java:691)
  7. at com.zsl.test.Test.main(Test.java:25)
  8. Caused by: <span style="color:#ff0000;">java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path</span> $
  9. at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
  10. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)

2.如果我现在将【一、3】中的

String jsonBean1="[{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0},{\"id\":1,\"name\":\"散仙0\<span>		</span>",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}]";  
改为:(去掉[ ])

String jsonBean1="{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0},{\"id\":1,\"name\":\"散仙0\<span>		</span>",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}";  

结果:

  1. Exception in thread "main" com.google.gson.JsonSyntaxException: <span style="color:#ff0000;">java.lang.IllegalStateException</span>: <span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;">Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path</span></span> $
  2. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:187)
  3. at com.google.gson.Gson.fromJson(Gson.java:805)
  4. at com.google.gson.Gson.fromJson(Gson.java:770)
  5. at com.google.gson.Gson.fromJson(Gson.java:719)
  6. at com.google.gson.Gson.fromJson(Gson.java:691)
  7. at com.zsl.test.Test.main(Test.java:25)
  8. Caused by: <span style="color:#ff0000;">java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path</span> $
  9. at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
  10. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)

三、看到以上两个结果都会报错,那么怎么解决呢?

1.判断返回的数据对应实体对象的个数

如果是一个就截取字符串用【json转实体对象】的方法实现:

<span style="white-space:pre">	</span>Person p=g.fromJson(jsonBean, Person.class);
如果是一个以上就直接用【json转List<实体对象>】的方法实现:

<span style="white-space:pre">	</span>List<Person> lists=g.fromJson(jsonBean1,new   TypeToken<List<Person>>(){}.getType());
四、附上Gson的下载连接地址:链接: http://pan.baidu.com/s/1pJI8b3P 密码: l3w3

/**
 * @author zsl
 * @link  http://blog.csdn.net/yy1300326388
 * @param 转载请注明
 * @version 2015年1月3日 19:48:59
 */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/246306
推荐阅读
相关标签
  

闽ICP备14008679号