码迷,mamicode.com
首页 > 移动开发 > 详细

android之GSON解析JSON

时间:2014-12-29 23:07:59      阅读:509      评论:0      收藏:0      [点我收藏+]

标签:android   json解析   gson解析   java      

Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。

比如:

<pre name="code" class="java">public class Order {
	public String id;
	public String OrderName;	
	@Override
	public String toString() {
		return "id:"+id+",OrderName:"+OrderName;
	}
}


public class GsonTest {
	public static void main(String[] args) {
		Order order=new Order();
		order.id="1";
		order.OrderName="Book";

		Gson gson=new Gson();
		String toJson=gson.toJson(order);
		System.out.println("Order--->JSON:\n"+toJson);
		Order fromJson=gson.fromJson(toJson,Order.class);
		System.out.println("JSON--->Order:\n"+fromJson);
	}	
}

输出:

Order--->JSON:
{"id":"1","OrderName":"Book"}
JSON--->Order:
id:1,OrderName:Book


接下来我们讲解GSON解析JSON的几种情况

1、模板反射对象

<span style="font-size:18px;">public class GsonTest {
	public static void main(String[] args) {
		List<Order> orderList=new ArrayList<Order>();
		Order order1=new Order();
		order1.id="1";
		order1.OrderName="Book";
		Order order2=new Order();
		order2.id="2";
		order2.OrderName="Fruit";
		orderList.add(order1);
		orderList.add(order2);
		
		Gson gson=new Gson();
		Type type=new TypeToken<List<Order>>(){}.getType();
		String toJson=gson.toJson(orderList,type);
		System.out.println("Order--->JSON:\n"+toJson);
		List<Order> fromJson=gson.fromJson(toJson,type);
		System.out.println("JSON--->Order:\n"+fromJson);
	}	
}</span>

输出:

Order--->JSON:
[{"id":"1","OrderName":"Book"},{"id":"2","OrderName":"Fruit"}]
JSON--->Order:
[id:1,OrderName:Book, id:2,OrderName:Fruit]


2、集合使用

public class GsonTest {
	public static void main(String[] args) {
		HashMap<String, Object> orderMap=new HashMap<String , Object>();
		Order order1=new Order();
		order1.id="1";
		order1.OrderName="Book";
		Order order2=new Order();
		order2.id="2";
		order2.OrderName="Fruit";
		orderMap.put("order1", order1);
		orderMap.put("order2", order2);
		
		Gson gson=new Gson();
		Type type=new TypeToken<HashMap<String,Object>>(){}.getType();
		String toJson=gson.toJson(orderMap,type);
		System.out.println("Order--->JSON:\n"+toJson);
		HashMap<String, Object> fromJson=gson.fromJson(toJson,type);
		System.out.println("JSON--->Order:\n"+fromJson);
	}	
}

输出:

Order--->JSON:
{"order2":{"id":"2","OrderName":"Fruit"},"order1":{"id":"1","OrderName":"Book"}}
JSON--->Order:
{order2={id=2, OrderName=Fruit}, order1={id=1, OrderName=Book}}


3、嵌套使用对象

public class Book {
	public String id;
	public String name;
	public String price;
}

public class Order {
	public String id;
	public String OrderName;	
	public ArrayList<Book> bookList;
	public HashMap<String, Book> bookMap;
}

public class GsonTest {
	public static void main(String[] args) {
		Book book1=new Book();
		book1.id="1001";
		book1.name="Java Think";
		book1.price="23";
		Book book2=new Book();
		book2.id="1002";
		book2.name="Gson Test";
		book2.price="45";
		HashMap<String, Book> orderMap=new HashMap<String , Book>();
		orderMap.put("orderMap1", book1);
		orderMap.put("orderMap2", book2);
		ArrayList<Book> orderList=new ArrayList<Book>();
		orderList.add(book1);
		orderList.add(book2);
		
		
		Order order=new Order();
		order.id="1";
		order.OrderName="Book";
		order.bookMap=orderMap;
		order.bookList=orderList;
		
		
		Gson gson=new Gson();
		Type type=new TypeToken<Order>(){}.getType();
		String toJson=gson.toJson(order,type);
		System.out.println("Order--->JSON:\n"+toJson);
		Order fromJson=(Order)gson.fromJson(toJson,type);
		System.out.println("JSON--->Order:\n"+fromJson);
	}	
}

输出:

Order--->JSON:
{"id":"1","OrderName":"Book","bookList":[{"id":"1001","name":"Java Think","price":"23"},{"id":"1002","name":"Gson Test","price":"45"}],"bookMap":{"orderMap2":{"id":"1002","name":"Gson Test","price":"45"},"orderMap1":{"id":"1001","name":"Java Think","price":"23"}}}
JSON--->Order:
Gson.Order@181eda8



转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/42242863    情绪控_



android之GSON解析JSON

标签:android   json解析   gson解析   java      

原文地址:http://blog.csdn.net/hai_qing_xu_kong/article/details/42242863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!