Android's Gson parsing json

Source: Internet
Author: User

Gson is a Java class library provided by Google for mapping between Java objects and JSON data.

Like what:

<pre name= "code" class= "Java" >public class Order {public String id;public string ordername; @Overridepublic string T Ostring () {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);}}

Output:

Order--->json:
{"id": "1", "Ordername": "Book"}
JSON--->order:
Id:1,ordername:book


Next we explain the Gson parsing JSON

1. Template Reflection Object

<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>

Output:

Order--->json:
[{"id": "1", "Ordername": "book"},{"id": "2", "Ordername": "Fruit"}]
JSON--->order:
[Id:1,ordername:book, Id:2,ordername:fruit]


2. Collection use

public class Gsontest {public static void main (string[] args) {hashmap<string, object> ordermap=new Hashmap<stri Ng, 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

Output:

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. Nested Use objects

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 Thi NK "; 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); o Rdermap.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);}}

Output:

Order--->json:
{"id": "1", "Ordername": "Book", "Booklist": [{"id": "1001", "Name": "Java Think", "Price": "All"},{"id": "1002", "Name": " Gson test "," Price ":" ""}], "Bookmap": {"orderMap2": {"id": "1002", "Name": "Gson Test", "Price": "$"}, "OrderMap1": {"id" : "1001", "Name": "Java Think", "Price": "23"}}
JSON--->order:
[Email protected]



Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/42242863 Emotional Control _



Android's Gson parsing json

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.