赞
踩
- package com.zsl.test;
-
- /**
- * 实体类
- * @author zsl
- * @version 2015年1月3日 19:10:10
- *
- */
- public class Person {
-
- private int id;
- private String name;
- private String address;
- private String email;
- private double money;
-
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return "编号:"+id+" 名字:"+name+" 地址:"+address+" E-mail:"+email+" 钱:"+money;
- }
- public Person() {
- // TODO Auto-generated constructor stub
- }
- public Person(int id, String name, String address, String email,
- double money) {
- super();
- this.id = id;
- this.name = name;
- this.address = address;
- this.email = email;
- this.money = money;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public double getMoney() {
- return money;
- }
- public void setMoney(double money) {
- this.money = money;
- }
-
-
- }
- /**
- * 转换一个Java Bean的时候
- * 记住不能使用[],只有在使用集合的时候才可以使用
- * 如上图贴的那个异常就是由此原因造成的,另外
- * 格式要规范,中间不能出现空格字符
- *
- **/
- Gson g=new Gson();
- String jsonBean="<span style="color:#ff0000;">{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"54152541@qq.com\",\"money\":500.0}</span>";
- Person p=g.fromJson(jsonBean, Person.class);
- System.out.println(p);
结果:
编号:0 名字:散仙0 地址:北京市0 E-mail:54152541@qq.com 钱:500.0
<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); }
- 编号:0 名字:散仙0 地址:北京市0 E-mail:54152541@qq.com 钱:500.0
- 编号:1 名字:散仙0 地址:北京市0 E-mail:54152541@qq.com 钱:500.0
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>";
- 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> $
- at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:187)
- at com.google.gson.Gson.fromJson(Gson.java:805)
- at com.google.gson.Gson.fromJson(Gson.java:770)
- at com.google.gson.Gson.fromJson(Gson.java:719)
- at com.google.gson.Gson.fromJson(Gson.java:691)
- at com.zsl.test.Test.main(Test.java:25)
- Caused by: <span style="color:#ff0000;">java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path</span> $
- at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
- at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
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}";
结果:
- 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> $
- at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:187)
- at com.google.gson.Gson.fromJson(Gson.java:805)
- at com.google.gson.Gson.fromJson(Gson.java:770)
- at com.google.gson.Gson.fromJson(Gson.java:719)
- at com.google.gson.Gson.fromJson(Gson.java:691)
- at com.zsl.test.Test.main(Test.java:25)
- Caused by: <span style="color:#ff0000;">java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path</span> $
- at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
- 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。