Android Data parsing-json parsing

Source: Internet
Author: User
Tags ming tojson

JSON (JavaScript Object Notation) is a lightweight data interchange format based on a subset of JavaScript (Standard ECMA-262 3rd edition-december 1999). JSON in a completely language-independent text format, Web development in this buddy often appear in your program, you also have to parse, generally speaking of JSON, will be involuntarily compared to its predecessor Xml,xml from the beginning of the design to the present ten years, In some aspects of the role has been beyond the original intention of the design of XML at the time, WebService is the main battlefield of XML, in the network application scenario due to the XML itself in the form of tags and DOM data, the resolution is relatively cumbersome. This time Jdouglas Crockford proposed JSON, do not write do not know, just know the last time to buy the "JavaScript language essence" is also this goods written, ⊙﹏⊙ ~ said more, enter the topic:

JSON and XML

Readability: JSON and XML are the same readability, JSON format commonly used symbols are commas, parentheses, colons, brackets, quotes five, relatively concise, XML specification of the label form, are easy to read and understand.

Extensibility: XML is inherently well-extensible, JSON has, nothing is XML extensible, and JSON is not extensible. However, JSON in the JavaScript home combat, can store JavaScript compound object, with XML incomparable advantage.

Coding Difficulty: XML has a wealth of coding tools, such as dom4j, jdom, etc., JSON also has the tools provided. Without tools, writing JSON is a little bit faster than XML (to me), and XML documents have many more characters on the structure.

JSON data format:

The form of a key-value pair: {"Name": "Flyelephant"}

An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) between the values, for example: [{"Name": "Flyelephant"},{"name": "Sananoro"}]

The value(value) can be a string enclosed in double quotation marks (string), a numeric value (number),true, false, NULL, an object, or an array. These structures can be nested .

JSON parsing

JSON format is fixed, it means that you can follow the rules of their own interpretation, but in order to improve the development efficiency, some tools provide a simple function for everyone to use, such as Json-lib,gson, since it is engaged in Android, using GSONJ parsing, gsonhttps:// code.google.com/p/google-gson/downloads/list?can=1&q=, download the latest one and add to your project ~

Whether it is development or testing is inseparable from the class object, so this time first built a book class:

public class Book (string BookName, string author) {super (); BookName = BookName; Author = Author;} private string  bookname;private string  author;public string Getbookname () {return bookname;} public void Setbookname (String bookname) {bookname = BookName;} Public String Getauthor () {return Author;} public void Setauthor (String author) {author = author;}}

To convert a class object:

Book Book=new Book ("Those things in the Ming Dynasty", "The Moon of the Year"); Gson gson=new Gson (); Generate JSON call ToJson System.out.println ("book:" + Gson.tojson (book)); Parse JSON call Fromjson book Jsonbook=gson.fromjson (Gson.tojson (book), Book.class); System.out.println ("Name:" +jsonbook.getbookname () + "--author:" +jsonbook.getauthor ());

  The results are as follows:

Book: {"BookName": "Those things in the Ming Dynasty", "Author": "The Moon of the Year"}name: The Ming Dynasty Those things--author: the Moon

Common use of list<string> and classes

Gson Gson = new Gson (); list<string> list = new arraylist<string> (); List.add ("Beijing"); List.add ("Shanghai"); List.add ("Guangzhou"); List.add ("Shenzhen"); System.out.println ("It City:" + Gson.tojson (list));//Note Here the second parameter list = Gson.fromjson (Gson.tojson (list), new TypeToken <List<String>> () {}.gettype ()); for (string string:list) {System.out.println (string);} Book Book=new Book (Gson.tojson (list), "The Moon of the Year"); System.out.println ("book:" + Gson.tojson (book)); Book Jsonbook=gson.fromjson (Gson.tojson, Book.class); System.out.println ("Name:" +jsonbook.getbookname () + "--author:" +jsonbook.getauthor ());

The results are as follows:

It city: ["Beijing", "Shanghai", "Guangzhou", "Shenzhen"] Beijing Shanghai Guangzhou Shenzhen book: {"BookName": "[\" "Beijing \", \ "Shanghai \", "Guangzhou \", "Shenzhen \"] "," Author ":" The Moon "}name:[" Beijing ", "Shanghai", "Guangzhou", "Shenzhen"]--author: Moon of the Year

 List<book> parsing:

Gson Gson = new Gson (); list<book> list = new arraylist<book> (); Book Book1 = new book ("Those things in the Ming Dynasty", "The Moon of the Year"); Book Book2 = new book ("Grave Tomb Notes", "South three Uncles"); Book BOOK3 = new book ("Ghost Blows", "World Bully Singing"), List.add (Book1); List.add (BOOK2); List.add (BOOK3); System.out.println ("Books:" + Gson.tojson (list)); list<book> listjsonbooks = Gson.fromjson (Gson.tojson (list), new typetoken<list<book>> () {}.getType ()); for (book book:listjsonbooks) {System.out.println ("Name:" + book.getbookname () + "Author:" + book.getauthor ());}

 The results are as follows:

books:[{"BookName": "Those things in the Ming Dynasty", "Author": "The Moon"},{"BookName": "Grave tomb Notes", "Author": "Three uncles of the South"},{"BookName": "Ghost Blows", "Author": " The World PA Sing "}]name: The Ming Dynasty Those things author: the Moon Name: Tomb Grave Note author: South Three uncle name: Ghost Blows Author: the World PA Sing

List<map<string,string>> parsing:

Gson Gson = new Gson (); list<map<string, string>> listmaps = new arraylist<map<string, string>> (); map<string, string> map1 = new hashmap<string, string> (); Map1.put ("Name", "flyelephant"); Map1.put ("Address", "on the ground"); map<string, string> map2 = new hashmap<string, string> (); Map2.put ("Name", "Sananoro"); Map2.put ("Address", "Beijing"); Map2.put ("Age", "25"); Listmaps.add (MAP1); Listmaps.add (MAP2); System.out.println ("Map:" +gson.tojson (Listmaps)); Listmaps=gson.fromjson (Gson.tojson (listmaps), new typetoken<list<map<string,string>>> () {}. GetType ()); For (map<string, string> map:listmaps) {System.out.println ("Name:" +map.get ("name") + "--address:" +map.get (" Address ")); }

The results are as follows:

map:[{"Address": "Place", "name": "Flyelephant"},{"Address": "Beijing", "age": "+", "name": "Sananoro"}]name:flyelephant--address: On the ground name: Sananoro--address: Beijing

  List<book> and Map<string> parse together:

Gson Gson = new Gson (); list<book> list = new arraylist<book> (); Book Book1 = new book ("Those things in the Ming Dynasty", "The Moon of the Year"); Book Book2 = new book ("Grave Tomb Notes", "South three Uncles"); Book BOOK3 = new book ("Ghost Blows", "World Bully Singing"), List.add (Book1); List.add (BOOK2); List.add (BOOK3); System.out.println ("Books:" + Gson.tojson (list)); list<book> listjsonbooks = Gson.fromjson (Gson.tojson (list), new typetoken<list<book>> () {}.getType ()); for (book book:listjsonbooks) {System.out.println ("Name:" + book.getbookname () + "Author:" + book.getauthor ());} map<string, string> map = new hashmap<string, string> (), Map.put ("list", Gson.tojson (list)), Map.put ("Info" , "on the ground"); SYSTEM.OUT.PRINTLN ("Mixed use:" +gson.tojson (map)); map = Gson.fromjson (Gson.tojson (map), New typetoken<map<string, String>> () {}.gettype ()); System.out.println ("list:" + map.get ("list") + "--info:" +map.get ("Info")); Listjsonbooks= Gson.fromjson (Map.get ("List"), new Typetoken<list<book>> () {}.gettype ()); Listjsonbooks) {SysteM.out.println ("Name:" + book.getbookname () + "Author:" + book.getauthor ());} 

The results are as follows:

books:[{"BookName": "Those things in the Ming Dynasty", "Author": "The Moon"},{"BookName": "Grave tomb Notes", "Author": "Three uncles of the South"},{"BookName": "Ghost Blows", "Author": " The World PA Sing "}]name: The Ming Dynasty Those things author: the Moon Name: Tomb Grave Note author: South of three uncle name: Ghost Blows Author: The World Pa Sing mixed use: {" List ":" [{\ "bookname\": \ "Those things in the Ming Dynasty", \ " Author\ ": \" the Moon \ "},{\" bookname\ ": \" tomb grave notes \ ", \" author\ ": \" Three uncles of the South "},{\" bookname\ ": \" ghost blows \ ", \" author\ ": \" world PA Sing \ "}]", " Info ":" On the ground "}list:[{" bookname ":" Those things in the Ming Dynasty "," Author ":" The Moon "},{" BookName ":" Grave tomb Notes "," Author ":" Three uncles of the South "},{" BookName ":" Ghost Blows "," Author ":" The World Pa Sing "}]--info: On the ground name: Ming those things Author: the Moon Name: Tomb Grave Note Author: South three Uncle Name: Ghosts blows Author: The World PA Sing

Weekend in the blog Park to see the articles are motivated children, we have a good Weekend ~

Android Data parsing-json parsing

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.