http://www.blogjava.net/qileilove/archive/2014/06/13/414694.html
First, JSONJSON (JavaScript Object Notation) is a lightweight data interchange format. JSON takes a completely language-independent text format. These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate. JSON vs. XML (referenced by: Sanpintian's CSDN blog): 1, Readability: JSON and XML readability is comparable, one side is the proposed syntax, one side is the canonical label form, it is difficult to distinguish the outcome. 2, Extensibility: XML is inherently very good extensibility, json of course, there is no XML can be extended, JSON can not. 3, coding Difficulty: XML has a wealth of coding tools, such as dom4j, Jdom, and so on, JSON also has json.org provides tools, but JSON encoding is significantly easier than XML, even if the use of tools can write JSON code, but to write good XML is not easy. 4, decoding difficulty: XML parsing to consider child node parent node, make people dizzy, and JSON parsing difficulty is almost 0. This is a little bit of XML output. 5, Popularity: XML has been widely used in the industry, and JSON has only just begun, but in the specific area of Ajax, the future development must be XML let in JSON. Ajax should become Ajaj (asynchronous Javascript and JSON).
Second, guide packageThe following packages need to be imported using JSON: Commons-beanutils.jar commons-logging.jar commons-lang.jar Commons-collection.jar Ezmorph.jar Json-li B.jar
Third, the realizationThe implementation of the book class:
1 public class Book {2 private String name;3 &nbs P Private double price;45 public String GetName () {6 return name;7 } public void SetName (String name) {Ten this.name = name;11 }1213 public double GetPrice () {$ return price;15 }1617 p ublic void Setprice (double price) { This.price = price;19 }2021 @Override22 Public String toString () {$ return name+ "" +price;24 }25} |
1/**2 * @author ZWQ3 * **/4 public class Jsontest {5 public static void main (string[] args) {6 //***********************************************************************7 //JSON basic Test 8 {9 //Create a new JSON array Jsonarray Jsonarray = new Jsonarray (); //New A JSON object Jsonobject json1 = new Jsonobject (); &N Bsp Json1.put ("id", "1"), + json1.put ("name", "Zhang San"); & nbsp Json1.put ("Password", "123456"), //Add a JSON object to the JSON array nbsp Jsonarray.add (Json1), 1819 Jsonobject json2 = new JSO Nobject (); 20 json2.put ("id", "2"), + json2.put ("NA Me "," John Doe "), json2.put (" Password "," 654321 "); &NB Sp Jsonarray.add (Json2), SYSTEM.OUT.PRINTLN ("JSON array basic test:" + Jsonarray); 2526 //results: + //[{"id": "1", "name": "Zhang San", "Password": "123456"},{"id": "2", "Name": "John Doe", "Password": "654321"}]28 }2930 //***********************************************************************31 //Convert object to JSON string + {+ Book book = new book (); 3 4 Book.setname ("Java Starter Tutorial"), Book.set Price (52.3), &NBSP; //Convert Java objects to JSON objects PNs Jsonobject jsonobject = Jsonobject.fromobject ( Book), SYSTEM.OUT.PRINTLN ("from Object to Jsonobject:" + jsonobject.tostring ()); 3940 //Results: $ //from object to jsonobject:{"NA Me ": Java Getting Started Tutorial", "Price": 52.3}42 }4344 //************************* //Convert a collection of objects into a JSON string-- & nbsp { list<book> List = new arraylist<book> (); Book Book1 = new book (), $ Book1.setname ("Advanced Math I"), Book1.setprice (34.1), a Book Book2 = new Boo K (); Book2.setname ("linear algebra"), Book2.setprice &N (12.7); Bsp List.add (Book1), List.add (BOOK2), + //Convert list collection to JSON array Jsonarray jsonarray= JSONARRAY.F Romobject (list), SYSTEM.OUT.PRINTLN ("from object collection to Jsonarray:" + Jsonarray.tostring ()) 5960 //results: + //From Object collection to jsonarray:[{"name": "Advanced Math I", "Price": 34.1},{"name": "Linear Algebra", "Price": 12.7}]62 }6364 //***********************************************************************65 //Convert JSON string to Java object-- {strin G jsonstring = "{name: ' data structure ', price:52.3}"; nbsp Jsonobject jsonobject = Jsonobject.fromobject (jsonstring); &NBSP ; //Convert a JSON object to Java object- Book book = (book) Jsonobject.tobean (Jsonobject, book . Class), SYSTEM.OUT.PRINTLN (book.tostring ()), 7273 /results:}7677 //Data structure 52.375 &N Bsp //***********************************************************************78 //Convert a JSON string to a Java object array {+- //(2). Array of beans String jsonsstring = "[{name: ' Database Basics ', Price:52.3},{name: ' Oracle 11g essence ' , price:42.3}] "; Jsonarray Jsonarray = Jsonarray.fromobject (jsonsstring); &n Bsp &NBSp Convert JSON array to Java object array book[] books = (book[]) Jsonarray.toarray (Jsonarray, book. Class), for (book B:books) {$ &NB Sp System.out.println (b.tostring ()); &NB }8889 Sp //Results: //Database Basics 52.391 //oracle 11g Essence 42.392 }93 }94} |
Iv. Analysis of the front end
1//jquery provides a way to get JSON 2//import Jquery34 $ (' #button ') before use. Click (function () {5//url: The requested address 6 $.getjson (' url ', {7 ID is required to pass the parameter 8 id:19}, function (data) {///successful after data is obtained JSON string 10///For example data:[{"name": "Advanced Mathematics I", "pric E ": 34.1},{" name ":" Linear Algebra "," Price ": 12.7}]11 alert (" The second book is: "+data[1].name+" Prices are: "+data[1].price); 12}); 13 }); |
Simple use and front-end parsing of JSON in Java