編輯:關於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) { ListorderList=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) { HashMaporderMap=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 ArrayListbookList; 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; HashMaporderMap=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
先放上一張效果圖:在這裡,我對自己的筆記本全屏截圖,然後當作自定義ImageView的src內容放在真機上運行。可以看到這裡的圖片是可以移動和縮放的。在這裡先說清一點,如
本例子包含若干shader文件,在項目assests文件夾下矩陣變換類package test.com.opengles7_4;import android.opengl
下拉菜單美團首頁類似的下拉彈出菜單工程中經常遇到的控件,不同工程中菜單條目的類型與數量也不一樣所以需要根據實際需要填充不同內容。先寫個demo,一倍不時之需吧。既然每個項
前面我們簡單的介紹過CoordinatorLayout這個控件,這一篇我們詳細地介紹這個ASD庫中最重要的控件。概述我們在NestedScrollView這個控件中有設置