What is a JSON?

1. Introduction: JavaScript Object Notation (JSON) is a NotationLightweight data interchange format. Easy to read and write. It is also easy for machine parsing and generation. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal format for data exchange.

2. A lightweight data interchange format

3. Lightweight is compared to XML. Json can also be used to store data, and the same data is much smaller and much faster to parse than XML.



The client uses JSON

The server uses JSON

Data is uploaded in JSON format

Use of Json in JavaScript. Json consists of key-value pairs surrounded by curly braces. Each key must be quoted, and the key and value are separated by colons. Multiple sets of key-value pairs are separated by commas.

Var = {” key “: value, value can be many types, number, string, JSON object, compare array, compare JSON array” key1 “: value};

How to access json objects: variable name.key

<script type="text/javascript"> var jsonObj = {"key1" : 12, // number "key2" : "ABC ", // string "key3" : [33, "333", true], / / array "key4" : {/ / json set of json "key4_1" : 41, "key4_2" : "string" 42}, "key5" : [{" key5_1_1 ": 511, "key5_1_2" : 512 },{ "key5_2_1" : 521, "key5_2_2" : 522 }] }; // json access // alert(jsonobj.key1); //12 // alert( jsonObj.key2 ); //abc // alert( jsonObj.key3[0] ); //33 // alert( jsonObj.key4.key4_2 ); //42String // alert( jsonObj.key5[1].key5_2_2 ); //522 // alert( jsonObj ); Var jsonString = json.stringify (jsonObj); alert(jsonString); /* {"key1":12,"key2":"abc","key3":[33,"333",true], "key4":{"key4_1":41,"key4_2":"42String"}, "key5":[{"key5_1_1":511,"key5_1_2":512}, {"key5_2_1":521," KEY5_2_2 ":522}]} */ // json string to json object var obj = json.parse (jsonString); alert(obj.key1); </script>Copy the code

Parse () : Converts a Json string to a Json object

To use Json in Java, import the JAR package first. Gson – 2.2.4. Jar

Javabeans and JSON interchange

@test public void test1() throws Exception {Person Person = new Person(1," Person is very cool!" ); Gson Gson = new Gson(); JsonString = gson.tojson (person); System.out.println( jsonString ); Person Person1 = gson.fromjson (jsonString, person.class); Person person1 = gson.fromjson (jsonString, person.class); System.out.println( person1 ); }Copy the code

Interchange of List and JSON

@Test public void test2() throws Exception { List<Person> list = new ArrayList<Person>(); List. add(new Person(1, "1 ")); List. add(new Person(2, "I ")); List. add(new Person(3, "1 ")); // Create a json utility class Gson Gson = new Gson(); // convert list toJson String String listJsonString = gson.tojson (list); System.out.println( listJsonString ); // List<Person> list2 = gson.fromJson(listJsonString, new PersonListType().getType()); List<Person> list2 = gson.fromJson(listJsonString, new TypeToken<ArrayList<Person>>() {}.getType()); System.out.println( list2 ); Person person = list2.get(0); System.out.println(person); }Copy the code

Map and JSON transfer

@Test public void test3() throws Exception { Map<String, Person> map = new HashMap<String, Person>(); Map. Put ("p1", new Person(1)); Map. Put ("p2", new Person(2, "I'm handsome, I love you!") )); Map. put("p3", new Person(3, "94V587")); Gson gson = new Gson(); String mapJsonString = gson.tojson (map); System.out.println( mapJsonString ); // the fromJson method if you want to return a javaBean object. Return Class type // To return a collection, To use the TypeToken class Map<String, Person> map2 = gson.fromJson(mapJsonString, new TypeToken<HashMap<String, Person>>(){}.getType()); System.out.println( map2 ); Person p = map2.get("p2"); System.out.println(p); }Copy the code

Case study:

@test public void Test () {Person Person = new Person(1," nice "); System.out.println("person:"+person); Gson gson = new Gson(); String json = gson.toJson(person); // Person class to json system.out.println ("person to json:"+json); Person person2 = gson.fromJson(json, Person.class); //json to Bean object system.out.println ("json to Person"+person2); List<Person> list = new ArrayList<Person>(); List. add(new Person(1, "1 ")); List. add(new Person(2, "I ")); List. add(new Person(3, "1 ")); System. The out. Println (" set the list: "+ list); String gson2 = gson.toJson(list); // Set to json system.out. println("list set to json: "+gson2); Map<String, Person> map = new HashMap<String, Person>(); Map. Put ("p1", new Person(1, "dragon ")); Map. Put ("p2", new Person(2, "I'm handsome, I love you!") )); Map. put("p3", new Person(3, "94V587")); System. The out. Println (" map collections: "+ map); String gString = gson.toJson(map); // Set to json system.out. println(" set to json:"+gString); Map<String, Person> map2 = gson.fromJson(gString,new TypeToken<HashMap<String, Person>>(){}.getType()); Println ("json to map:"+map2); //json to Bean system.out.println ("json to map:"+map2); }Copy the code

Results:

{"id":1,"name":" handsome "} json transform PersonPerson [id=1, name= handsome] list set :[person [id=1, name= handsome] Name = 1, Person [id=2, name= 2], Person [id=3, name= 3]] [{" id ": 1," name ":" elder brother handsome once "}, {" id ": 2," name ":" elder brother handsome secondary "}, {" id ": 3," name ":" elder brother handsome three times "}] map collections: {p1 = Person] [id = 1, name = dragon, P2 =Person [id=2, name= Person] , p3 = Person [id = 3, the elder brother of the name = 94 v587]} Map collections to json: {" p1 ": {" id" : 1, "name" : "dragon"}, "p2" : {" id ": 2," name ":" elder brother very handsome, I love you!" }, "p3" : {" id ": 3," name ":" elder brother 94 v587 "}} json to map: {p1 = Person] [id = 1, name = dragon, p2 = Person [id = 2, name = elder brother very handsome, I love you!] , p3=Person [id=3, name= 94V587]}Copy the code