Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android之GSON解析JSON

android之GSON解析JSON

編輯:關於Android編程

Gson 是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 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:
+toJson);
		Order fromJson=gson.fromJson(toJson,Order.class);
		System.out.println(JSON--->Order:
+fromJson);
	}	
}

 

輸出:

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

 

接下來我們講解GSON解析JSON的幾種情況

1、模板反射對象

 

public class GsonTest {
	public static void main(String[] args) {
		List orderList=new ArrayList();
		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>(){}.getType();
		String toJson=gson.toJson(orderList,type);
		System.out.println(Order--->JSON:
+toJson);
		List fromJson=gson.fromJson(toJson,type);
		System.out.println(JSON--->Order:
+fromJson);
	}	
}

輸出:

 

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 orderMap=new HashMap();
		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>(){}.getType();
		String toJson=gson.toJson(orderMap,type);
		System.out.println(Order--->JSON:
+toJson);
		HashMap fromJson=gson.fromJson(toJson,type);
		System.out.println(JSON--->Order:
+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 bookList;
	public HashMap 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 orderMap=new HashMap();
		orderMap.put(orderMap1, book1);
		orderMap.put(orderMap2, book2);
		ArrayList orderList=new ArrayList();
		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(){}.getType();
		String toJson=gson.toJson(order,type);
		System.out.println(Order--->JSON:
+toJson);
		Order fromJson=(Order)gson.fromJson(toJson,type);
		System.out.println(JSON--->Order:
+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

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved