天天看點

【第11篇】通過GSON的TreeReader與JsonReader去處理json資料

package ivyy.taobao.com.domain.gson;

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;

/**
 *@Date:2015-1-5
 *@Author:liangjilong
 *@Email:[email protected]
 *@Version:1.0
 *@Description:
 */
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("country", "中國");
		 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", "[email protected]"); 
		 
		 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();
	}

}