Simple use of JSON in Java and front-end Parsing

Source: Internet
Author: User

I. JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format. JSON is in a language-independent text format. These features make JSON an ideal data exchange language. Easy for reading and writing, and easy for machine parsing and generation.

Comparison between JSON and XML (referenced from sanpintian's CSDN blog ):

1. Readability: the readability of JSON and XML is comparable. One side is the suggested syntax and the other side is the standard label format, which makes it difficult to distinguish between the two.

2. Scalability: XML is inherently highly scalable, and JSON is also available. There is nothing XML can be extended, and JSON cannot.

3. Encoding difficulty: XML has a variety of encoding tools, such as Dom4j and JDom. JSON also provides tools provided by json.org, but JSON encoding is much easier than XML, JSON code can be written without tools, but it is not easy to write XML.

4. Difficulty in decoding: the parsing of XML should take into account the parent node of the child node. The JSON parsing difficulty is almost 0. There is nothing to say about XML.

5. Popularity: XML has been widely used in the industry, and JSON has just begun. However, in the specific field of Ajax, the future development of XML must be in JSON. Then Ajax should be changed to Ajaj (Asynchronous Javascript and JSON.

 

Ii. guide package

To use JSON, You need to import the following packages:

Commons-beanutils.jar
Commons-logging.jar
Commons-lang.jar
Commons-collection.jar
Ezmorph. jar
Json-lib.jar

Iii. Implementation

 

Book class implementation:

1 public class Book { 2 private String name; 3 private double price; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) {10 this.name = name;11 }12 13 public double getPrice() {14 return price;15 }16 17 public void setPrice(double price) {18 this.price = price;19 }20 21 @Override22 public String toString() {23 return name+" "+price;24 }25 }

 

1/*** @ author ZWQ 3 ***/4 public class JsonTest {5 public static void main (String [] args) {6 //************************************ * test 8 {9 // create a new JSON array 10 JSONArray jsonArray = new JSONArray (); 11 // create a JSON object 12 JSONObject json1 = new JSONObject (); 13 json1.put ("id", "1"); 14 json1.put ("name", "Zhang San "); 15 json1.put ("password", "123456"); 16 // Add the JSON object to the JSON array to go to 17 jsonArray. add (json1); 18 19 JSONObject json2 = new JSONObject (); 20 json2.put ("id", "2"); 21 json2.put ("name", "Li Si "); 22 json2.put ("password", "654321"); 23 jsonArray. add (json2); 24 System. out. println ("basic json array test:" + jsonArray); 25 26 // result: 27 // [{"id": "1", "name": "James ", "password": "123456" },{ "id": "2", "name": "Li Si", "password ": "654321"}] 28} 29 30 //***************************** **************************************** ** 31 // convert the object to a JSON string 32 {33 Book book = new Book (); 34 book. setName ("Java getting started tutorial"); 35 book. setPrice (52.3); 36 // convert a Java object to a JSON object 37 JSONObject jsonObject = JSONObject. fromObject (book); 38 System. out. println ("from Object to JSONObject:" + jsonObject. toString (); 39 40 // result: 41 // from Object to JSONObject: {"name": "Java getting started tutorial", "price ": 52.3} 42} 43 44 //******************************** ************************************* 45 // convert the object set to a JSON string 46 {47 List <Book> list = new ArrayList <Book> (); 48 Book book1 = new Book (); 49 book1.setName ("Advanced Mathematics I"); 50 book1.setPrice (34.1); 51 Book book2 = new Book (); 52 book2.setName ("Linear Algebra"); 53 book2.setPrice (12.7); 54 list. add (book1); 55 list. add (book2); 56 // convert the List set to a JSON array 57 JSONArray jsonArray = JSONArray. fromObject (list); 58 System. out. println ("from Object collection to JSONArray:" + jsonArray. toString (); 59 60 // result: 61 // from the Object set to JSONArray: [{"name": "Advanced Mathematics I", "price": 34.1 }, {"name": "Linear Algebra", "price ": 12.7}] 62} 63 64 //******************************* **************************************** 65 // convert the JSON String into a Java object 66 {67 String jsonString = "{name: 'Data struct', price: 52.3} "; 68 JSONObject jsonObject = JSONObject. fromObject (jsonString); 69 // convert the JSON object into a Java object 70 Book book = (Book) JSONObject. toBean (jsonObject, Book. class); 71 System. out. println (book. toString (); 72 73 // result: 74 // data structure 52.375} 76 77 //***************************** **************************************** ** 78 // convert the JSON string into a Java object array 79 {80 // (2 ). bean array 81 String jsonsString = "[{name: 'database basics ', price: 52.3}, {name: 'oracle 11gb', price: 42.3}]"; 82 JSONArray jsonArray = JSONArray. fromObject (jsonsString); 83 // convert the JSON array into a Java object array 84 Book [] books = (Book []) JSONArray. toArray (jsonArray, Book. class); 85 for (Book B: books) {86 System. out. println (B. toString (); 87} 88 89 // result: 90 // database base 52.391 // Oracle 11g essence 42.392} 93} 94}

 

Iv. Front-end analysis

 

1 // method provided by Jquery for obtaining json 2 // before using Jquery 3 4 $ (# button '). click (function () {5 // url: request address 6 $. getJSON ('url', {7 // id is the required parameter 8 id: 1 9}, function (data) {// after success, data is the obtained json string 10 // For example, data: [{"name": "Advanced Mathematics I", "price": 34.1 }, {"name": "Linear Algebra", "price": 12.7}] 11 alert ("second book:" + data [1]. name + "Price:" + data [1]. price); 12}); 13 });

 

V. Attachment

: Http://pan.baidu.com/s/1sjvxZXF

File introduction:

Jsondemo.zip: source code of jsondemo

 

 

Related Article

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.