当前位置: 代码迷 >> JavaScript >> gson的JsonTreeReader跟JsonReader一起使用代码详解例子
  详细解决方案

gson的JsonTreeReader跟JsonReader一起使用代码详解例子

热度:211   发布时间:2013-08-01 15:23:18.0
gson的JsonTreeReader和JsonReader一起使用代码详解例子
package com.app.json;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.internal.bind.JsonTreeReader;
import com.google.gson.stream.JsonReader;

/**
 *@DEMO:napp
 *@Author:jilongliang
 *@Date:2013-7-21
 */
public class GsonTreeReader {
	private static Gson gson = new Gson();
	public static void main(String[] args) throws Exception {
		/**
		  * 组装address对象的数据
		  */
		 Map<String, String> address = new HashMap <String, String>(); 
		 address.put("province", "广东省");
		 address.put("city", "广州市");
		 address.put("district", "天河区");
		 address.put("street", "棠东"); 
		 /**
		  * 组装users对象的数据
		  */
		 Map<String, String> users = new HashMap <String, String>(); 
		 users.put("username", "liangjilong");
		 users.put("age", "25");
		 users.put("tel", "12396878");
		 users.put("email", "jilongliang@sina.com"); 
		 
		 Map<Object, Object> listsObj = new HashMap <Object, Object>(); 
		 listsObj.put("address",address);//address节点
		 listsObj.put("users",users);//users节点
		 
		 Object obj=listsObj;//转换成对象
		 
		 Map<Object, Object> info = new HashMap <Object, Object>(); 
		 info.put("info", obj);//json的根节点
		 
		 
        String json=gson.toJson(info);//转换成json数据
        System.out.println(json);//打印json数据
		
		readJsonData(json);
		
	}
	/**JsonReader
	 * JsonTreeReader从文件读取json数据
	 * @param in
	 * @throws Exception
	 */
	public static void readJsonData(String json) throws Exception {
		//创建JsonParser对象
		JsonParser parser = new JsonParser();
		JsonElement jsonEl = parser.parse(json);
		//创建一个JsonTreeReader对像用JsonReader
		JsonReader reader = new JsonTreeReader(jsonEl);
		try {
			reader.beginObject();
			String tagName = reader.nextName();
			if (tagName.equals("info")) {
				readInfo(reader);
			}
			reader.endObject();
		} finally {
			reader.close();
		}
	}
	/**
	 * 读取json的父(根,第一个)节点
	 * @param reader
	 * @throws Exception
	 */
	public static void readInfo(JsonReader reader) throws Exception {
		reader.beginObject();
		while (reader.hasNext()) {
			String tagName = reader.nextName();
			if (tagName.equals("address")) {
				readAddress(reader);
			} else if (tagName.equals("users")) {
				readUsers(reader);
			} 
		}
		reader.endObject();
	}
	/**
	 * 读取用户信息值
	 * @param reader
	 * @throws IOException
	 */
	public static void readUsers(JsonReader reader) throws IOException {
		reader.beginObject();
		while (reader.hasNext()) {
			String tag = reader.nextName();
			if (tag.equals("username")) {
				String username = reader.nextString();
				System.out.println("用户名:" + username);
			} else if (tag.equals("email")) {
				String email = reader.nextString();
				System.out.println("Email:" + email);
			} else if (tag.equals("tel")) {
				String tel = reader.nextString();
				System.out.println("电话:" + tel);
			}else if (tag.equals("age")) {
				String age = reader.nextString();
				System.out.println("年龄:" + age);
			} else {
				reader.skipValue();//忽略值/跳过break
			}
		}
		reader.endObject();
	}
	/**
	 * 读取地区值
	 * @param reader
	 * @throws IOException
	 */
	public static void readAddress(JsonReader reader) throws IOException {
		reader.beginObject();
		while (reader.hasNext()) {
			String tag = reader.nextName();
			if (tag.equals("province")) {
				String province = reader.nextString();
				System.out.println("省份:" + province);
			} else if (tag.equals("city")) {
				String city = reader.nextString();
				System.out.println("街道:" + city);
			} else if (tag.equals("street")) {
				String street = reader.nextString();
				System.out.println("街道:" + street);
			}else if (tag.equals("district")) {
				String district = reader.nextString();
				System.out.println("区:" + district);
			} else {
				reader.skipValue();//忽略值/跳过break
			}
		}
		reader.endObject();
	}

}

?

package com.app.entity.duapp;

import java.io.Serializable;

import com.google.gson.annotations.Expose;
/**
 * Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
 * 创建Gson对象,没有@Expose注释的属性将不会被序列化
 * serialize序列化 默认 true
 * deserialize反序列化 默认 true
 */
public class Address implements Serializable{
	@Expose(serialize=false)// 序列化  /Address类已经序列化
	private int id;
	@Expose(deserialize=false)// 反序列化
	private String country;//国家
	private String province;//省份
	private String city;//城市
	private String street;//街道
	private String district;//地区
	private String cityCode;//邮政编码
	private String streetNumber;//街道号
	
	public Address() {
	}
	
	public Address(int id,String country) {
		this.id=id;
		this.country=country;
	}
	public String getCityCode() {
		return cityCode;
	}
	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}
	public String getStreetNumber() {
		return streetNumber;
	}
	public void setStreetNumber(String streetNumber) {
		this.streetNumber = streetNumber;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getDistrict() {
		return district;
	}
	public void setDistrict(String district) {
		this.district = district;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
}

?