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

一、Gson的使用:

1.实体类

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;  
    }  
      
  
}  

2.json转换为实体对象

	/** 
         * 转换一个Java Bean的时候 
         * 记住不能使用[],只有在使用集合的时候才可以使用  
         * 如上图贴的那个异常就是由此原因造成的,另外 
         * 格式要规范,中间不能出现空格字符 
         *  
         **/
	Gson g=new Gson();
	String jsonBean="{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0}";  
        Person p=g.fromJson(jsonBean, Person.class);
        System.out.println(p); 

结果:

编号:0  名字:散仙0   地址:北京市0   E-mail:[email protected]  钱:500.0


3.json转换为List<Person

	Gson g=new Gson();
String jsonBean1=" [{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0},{\"id\":1,\"name\":\"散仙0\ ",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0}]"; List lists=g.fromJson(jsonBean1,new TypeToken>(){}.getType()); for(Person pp:lists){ System.out.println(pp); }
 
  
j结果:

编号:0  名字:散仙0   地址:北京市0   E-mail:[email protected]  钱:500.0
编号:1  名字:散仙0   地址:北京市0   E-mail:[email protected]  钱:500.0

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

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

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

String jsonBean="[{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0}]";

结果:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	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: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)

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

String jsonBean1="[{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0},{\"id\":1,\"name\":\"散仙0\		",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0}]";  
改为:(去掉[ ])

String jsonBean1="{\"id\":0,\"name\":\"散仙0\",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0},{\"id\":1,\"name\":\"散仙0\		",\"address\":\"北京市0\",\"email\":\"[email protected]\",\"money\":500.0}";  

结果:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	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: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)

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

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

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

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

	List lists=g.fromJson(jsonBean1,new   TypeToken>(){}.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
 */

你可能感兴趣的:(Google,Util)