(尚学堂)解析json

 

package com.atguigu.l05_handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.test.AndroidTestCase;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/*
1. 将json格式的字符串{}转换为Java对象, 使用原生API
2. 将json格式的字符串{}转换为Java对象, 使用GSON
3. 将json格式的字符串[]转换为Java对象的List, 使用原生API
4. 将json格式的字符串[]转换为Java对象的List, 使用GSON

5. 将Java对象转换为json字符串{}, 使用GSON
6. 将Java对象的List转换为json字符串[], 使用GSON
 */
public class JsonTest extends AndroidTestCase{

	/*
	 * 1. 将json格式的字符串{}转换为Java对象, 使用原生API
	 */
	public void testJsonToObject() throws JSONException {
		String jsonString = "{\"id\":2, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}";
		
		//将json字符串封装为JSONObject对象
		JSONObject jsonObject = new JSONObject(jsonString);
		//从对象中根据key得到对应的value
		int id = jsonObject.getInt("id");
		String name = jsonObject.getString("name");
		double price = jsonObject.getDouble("price");
		String imagePath = jsonObject.getString("imagePath");
		//封装ShopInfo对象
		ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath);
		
		Log.e("TAG", shopInfo.toString());
	}
	
	/*
	 * 1. 将json格式的字符串{}转换为Java对象, 使用GSON
	 */
	public void testJsonToObject2()  {
		String jsonString = "{\"id\":3, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}";
		
		ShopInfo shopInfo = new Gson().fromJson(jsonString, ShopInfo.class);
		
		Log.e("TAG", shopInfo.toString());
	}
	
	
	/*
	 * 3. 将json格式的字符串[]转换为Java对象的List, 使用原生API
	 */
	public void testJsonToList() throws JSONException {
		String jsonString = "[{\"id\":3, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"},"
				+ "{\"id\":5, \"name\":\"大虾2\", \"price\":128.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]";
		List<ShopInfo> list = new ArrayList<ShopInfo>();
		
		//1. 将json字符串包装JSONArray对象
		JSONArray jsonArray = new JSONArray(jsonString);
		//2. 遍历JSONArray对象所有元素(JSONObject), 并将每个元素封装为shopInfo, 并添加到List
		for(int i=0;i<jsonArray.length();i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			//从对象中根据key得到对应的value
			int id = jsonObject.getInt("id");
			String name = jsonObject.getString("name");
			double price = jsonObject.getDouble("price");
			String imagePath = jsonObject.getString("imagePath");
			//封装ShopInfo对象
			ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath);
			list.add(shopInfo);
		}
		
		Log.e("TAG", list.toString());
	}
	
	/*
	 * 4. 将json格式的字符串[]转换为Java对象的List, 使用GSON
	 */
	public void testJsonToList2() throws JSONException {
		String jsonString = "[{\"id\":4, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"},"
				+ "{\"id\":6, \"name\":\"大虾2\", \"price\":128.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]";
		
		List<ShopInfo> list = new Gson().fromJson(jsonString, new TypeToken<List<ShopInfo>>(){}.getType());
		
		Log.e("TAG", list.toString());
	}
	
	/*
		5. 将Java对象转换为json字符串{}, 使用GSON
	*/
	public void testObjectToJson() {
		ShopInfo info = new ShopInfo(3, "KK", 1000, "http://www.sina.com");
		String json = new Gson().toJson(info);
		Log.e("TAG", json);
	}
	
	
	/*
		6. 将Java对象的List转换为json字符串[], 使用GSON
	*/
	
	public void testListToJson() {
		
		List<ShopInfo> list = new ArrayList<ShopInfo>();
		list.add(new ShopInfo(3, "KK", 1000, "http://www.sina.com"));
		list.add(new ShopInfo(4, "KK2", 2000, "http://www.sina.com222"));
		
		String json = new Gson().toJson(list);
		
		Log.e("TAG", json);
	}
	
	public void testJsonToMap() {
		String jsonString = "{\"my name\":\"大虾\", \"1\":12}";
		Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<Map<String, Object>>(){}.getType());
		Log.e("TAG", map.toString());
	}
}
package com.atguigu.l05_handler;

public class ShopInfo {

	private int id;
	private String name;
	private double price;
	private String imagePath;

	public ShopInfo(int id, String name, double price, String imagePath) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.imagePath = imagePath;
	}

	public ShopInfo() {
		super();
	}

	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 double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	@Override
	public String toString() {
		return "ShopInfo [id=" + id + ", name=" + name + ", price=" + price
				+ ", imagePath=" + imagePath + "]";
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值