JavaScript Object Notation: Object representation of javascript.
This is a grammar that can pass objects, such as key-value pairs, arrays, and other objects.
Lightweight data transmission method.
json format:
Key:{key:{key:[{key: value, key: value}]} is flexible.
{}: Represents a collection, container
[]: Install arrays, collections
Key-value pairs are separated by colons, and elements of arrays are separated by commas.
He is not object-oriented, but more analytical and reusable than javascript. Both front and back ends are available.
The value of json can be: int float string Boolean array object.
json method:
JSON.parse(): This method is used to parse JSON type strings and return the corresponding values.
JSON.stringify(): This method also returns the JSON string corresponding to the specified value.
JavaScript can use the eval() method to convert json text into JavaScript objects.
var obj = eval ( " (" + json + " ) " );
Note: The eval() method of javascript can compile and execute any javascript code, but it's not safe. It's safer to use a json parser to convert json to javascript
json format conversion, objects, collections: (official json package and Alibaba json package)
Official: You can't get all the attributes of an object at one time. You can only get a single value at one time.
eg: user object: with name id
Convert JSONObject user Json = new JSONObject (user);
Get String userName = userJson. getString ("name");
int userId = userJson.getInt("age");
list of user collections
Conversion: JSONArray user JsonArray = new JSONArray (list);
Get: JSONObiect userObj = userJsonArray. get JSONObject (0);
Alibaba: fastJson package
eg: user object: This object can be obtained directly
Conversion: String userFast = JSON. to JSONString (user); - - JSON string
Get: User U1 = JSONObject. parseObject (fastUser, User. class);
User collection: This collection of objects can be obtained directly
Conversion: String fastList = new JSONArray (list);
Get: list < User > fastList = JSONArray. parseArray (fastList, User. class)
Note: Attention should be paid to method names when testing. Both of them have the same method names and need full name references.
1 import com.alibaba.fastjson.JSON; 2 import org.json.JSONArray; 3 import org.json.JSONException; 4 import org.json.JSONObject; 5 6 import java.util.ArrayList; 7 import java.util.List; 8 9 public class TestJson { 10 public static void main(String[] args) throws JSONException { 11 12 List<User> list = new ArrayList<>(); 13 14 User user1 = new User(12,"Zhang San"); 15 User user2 = new User(13,"Li Si"); 16 list.add(user1); 17 list.add(user2); 18 19 System.out.println("---------------------------"); 20 System.out.println(" Official jar Package testing"); 21 //org.json Transform Object Testing 22 JSONObject userObj = new JSONObject(user1); 23 //getString Need exception throw prevention key Value does not exist 24 System.out.println("Getting the object id Value: "+userObj.getString("id")); 25 //org.json Conversion Set Testing 26 JSONArray userList = new JSONArray(list); 27 //get The method takes the set subscript and needs to throw the exception that the subscript does not exist. 28 System.out.println("Get the value of the set subscribed to 0: "+userList.get(0)); 29 User user3 = (User) userList.get(0); 30 System.out.println("Output object:"+user3.toString()); 31 String userStr = new JSONObject(user1).toString(); 32 System.out.println("Direct Print Object:"+user1.toString()); 33 System.out.println("Object transformation json Character string:"+userStr); 34 System.out.println("Direct Print Collection: "+list); 35 //Object transformation json Character string 36 String userlistStr = new JSONArray(list).toString(); 37 System.out.println("Set Conversion json Character string: " + userlistStr); 38 System.out.println("---------------------------"); 39 40 //Alibaba's fastJson Package testing 41 System.out.println("---------------------------"); 42 System.out.println(" Alibaba fastJson Package testing"); 43 //com.alibaba.fastjson Package tests use full names because of method renaming 44 //Direct Conversion json Character string 45 String userFast = JSON.toJSONString(user1); 46 System.out.println("fastjson Conversion object to json Character string: "+userFast); 47 String userlistFast = JSON.toJSONString(list); 48 System.out.println("fastjson Direct Conversion Set to json Character string: "+userlistFast); 49 50 //json String conversion to json object 51 com.alibaba.fastjson.JSONObject userFast2 = JSON.parseObject(userFast); 52 System.out.println("fastjson take json String converted json Objects:"+userFast2); 53 //json String conversion to json aggregate 54 com.alibaba.fastjson.JSONArray userlistFast2 = JSON.parseArray(userlistFast); 55 System.out.println("fastjson take json String converted json Collection:"+userlistFast2); 56 } 57 }
--------------------------- Official jar package testing Get the id value of the object: 12 Get the set subscript 0: User{id=12, name ='Zhang San'} Output object: User{id=12, name ='Zhangsan'} Direct Print Object: User{id=12, name='Zhangsan'} Object transformation json string: {"name": "Zhang San", "id":12} Direct printing set: [User{id=12, name ='Zhang San'}, User{id=13, name ='Li Si'}] Set transformation json string: ["User{id=12, name ='Zhangsan'}," "User{id=13, name ='Lisi'}"] --------------------------- --------------------------- Alibaba fastJson package test fastjson converts the object to a json string: {"id":12,"name": "Zhang San"} fastjson directly converts the set to a json string: [{id":12,"name":"Zhang San"}, {id":13,"name": "Li Si"} fastjson converts json strings into json objects: {"name": "zhang san", "id":12} fastjson converts json sets of json strings: [{name":"Zhang San","id": 12}, {name", "Li Si", "id":13}]