Data Storage (iii)--json data processing

Source: Internet
Author: User

JSON is a lightweight data interchange format that has good readability and quick-to-write features that enable data exchange between different platforms. JSON uses a high-compatibility text format, and also has a similar behavior to the C language system. JSON can convert a Java object into a JSON-formatted string that converts a JSON string into Java. More lightweight than XML, JSON is lightweight and simple to use. JSON data format, which is widely used in Android for client and server communication, is very convenient for network data transmission and parsing.

jsonobject--This is the basic unit of JSON definition in the system, which contains a pair of child (Key/value) values.
jsonarray--it represents an ordered set of values. Converts it to a string output (toString) in the form of a square bracket wrapped with a value separated by a comma "," (for example: [Value1,value2,value3]
jsonstringer--This class can help create jsontext quickly and easily. Its greatest advantage is that it can reduce program exceptions due to format errors, which can automatically create JSON text in strict accordance with JSON syntax rules (syntaxrules). Each Jsonstringer entity can only create one JSON text.
Jsontokener--json Parsing class

Exceptions used in Jsonexception--json


The book object in data store (ii) is converted to string in JSON format.

public static String objecttojsonstring (List<book> Books) throws Jsonexception{jsonstringer Stringer = new Jsonstringer (); Stringer.object (); Stringer.key ("books"); Stringer.array (); for (int i=0;i<books.size (); i++) { Stringer.object (); Stringer.key ("Book:name"). Value (Books.get (i). GetName ()); Stringer.key ("Book:id"). Value ( Books.get (i). GetId ()), Stringer.key ("Book:price"). Value (Books.get (i). GetPrice ()); Stringer.key ("Book:publisher") . Value (Books.get (i). Getpublisher ()), Stringer.key ("Book:count"). Value (Books.get (i). GetCount ()); Stringer.endobject ();} Stringer.endarray (); Stringer.endobject (); return stringer.tostring ();}

String converted to book object

public static list<book> Jsonstringtoobject (String str) throws jsonexception{list<book> books = new Arraylist<book> (); Jsontokener Jsontokener = new Jsontokener (str); Jsonobject Jsonobject = (jsonobject) jsontokener.nextvalue (); Jsonarray array =jsonobject.getjsonarray ("books"), for (int i =0;i<array.length (), i++) {Book book = new book (); Jsonobject temp = ((jsonobject) array.get (i)), Book.setname (temp.getstring ("Book:name")), Book.setid (Temp.getstring ( "Book:id")); Book.setprice (temp.getstring ("Book:price")); Book.setpublisher (temp.getstring ("Book:publisher")); Book.setcount (Temp.optint ("Book:count")); Books.add (book);} return books;}
Note:

GetXXX () can convert the value of the key you want to get to the specified type, or throw jsonexception if it cannot be converted or has no value
The Optxxx () is also a value that is to be obtained when the value of the key is converted to the specified type, cannot be converted, or has no value when returned by the user or by default.


Data Storage (iii)--json data processing

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.