日期:2014-05-16  浏览次数:20647 次

灵活组装Json的数据使用Gson的JsonParser和JsonReader解析Json详解例子
package com.app.json;  
  
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;

import com.app.entity.duapp.Address;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 *@DEMO:napp
 *@Author:jilongliang
 *@Date:2013-7-20
 */
public class JsonParse {  
	private static Gson gson=new Gson();
    /**
	 * 通过JsonLib把数组转换成Json数据
	 * @param url
	 * @throws Exception
	 */
	private static JSONArray getArrayToJsonByJsonLib()throws Exception
	{
		JSONArray jsonArr=null;
		List<Address> lists=new ArrayList<Address>();
		for (int i = 1; i <=12; i++) {
			Address addr = new Address(); 
			addr.setProvince("第"+i+"个省份");
			addr.setCity("第"+i+"个城市");
			addr.setCityCode("130"+i);
			addr.setStreet("街道"+i);
			addr.setStreetNumber("街道号码"+i);
			lists.add(addr);
		}
		jsonArr = JSONArray.fromObject(lists);  //把对象转换成Json对象
		return jsonArr;
	}
	
	public static void main(String[] args)throws Exception{
		getJsonGson(); 
		
	}
	
	/**
	 * 利用Json-lib封装数组Json数据,Gson解析Json数据.
	 * @throws Exception
	 */
	private static void getJsonGson() throws Exception {
		//Json-lib的JSONArray数组对象
		JSONArray  JsonArray =getArrayToJsonByJsonLib();
		String jsonData=JsonArray.toString();
		/*
		 * 自己组装成一个json格式,格式必须为{,,}才能为JsonElement对象的parse方法解析不是否则
		 * 会报错com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 1307
		 */
		String json="{status:1,address:"+jsonData+"}";

		// 创建一个JsonParser
		JsonParser parser = new JsonParser();
		JsonElement jsonEl = parser.parse(json);
		
		JsonObject jsonObj = null;
		jsonObj = jsonEl.getAsJsonObject();//转换成Json对象
		String status=jsonObj.get("status").getAsString();//status节点
		//System.out.println(status);
		
		JsonElement addressEl=jsonObj.get("address");//address节点
		if(addressEl.isJsonArray()){//判断是否为Json数组
			JsonArray jsonArray= addressEl.getAsJsonArray();//转为数组
			for(Iterator iter=jsonArray.iterator();iter.hasNext();){
				JsonObject obj=(JsonObject) iter.next();
				//String city=obj.get("city").getAsString();//方法一:根据key形势取值
				Address add=gson.fromJson(obj, Address.class);//方法二:灵活使用Gson取数据
				System.out.println(add.getProvince()+"\t"+add.getCity());
			 }
		}
	}
	 
 } 

?json数据是由运行JsonRead类可以生成

{
	"info":
	{
		"users":{"username":"liangjilong","email":"jilongliang@sina.com","tel":"12396878","age":"25"},
		"address":{"street":"棠东","province":"广东省","district":"天河区","city":"广州市"}
	}
}

?JsonRead

package com.app.json;

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

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

/**
 *@DEMO:napp
 *@Author:jilongliang
 *@Date:2013-7-20
 */
public class JsonRead {
	private static Gson gson=new Gson();
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		 getJsonData();
         
        //方法二
        //String pathname="src/config/doc/json";
 		//InputStream input = new FileInputStream(new File(pathname));
 		//readJsonData(input,null);
	}
	/**
	 * 组装Json数据
	 * @throws Exception
	 */
	private static void getJsonData() 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.