Home>Article>Web Front-end> Example parsing the serialization and deserialization of Json objects and Json strings
This article brings you relevant knowledge aboutjavascript, which mainly introduces related issues about Json objects. JavaScript Object Notation is used to store and exchange text information syntax, and perform data processing. For transmission, JSON is smaller, faster, and easier to parse than XML. Let’s take a look at it. I hope it will be helpful to everyone.
[Related recommendations:javascript video tutorial,web front-end】
JavaScript Object Notation -JavaScript
Syntax for storing and exchanging text information for data transmission. JSON is smaller, faster, and easier to parse than XML.
var person = {"name": "张三", age: 23, 'gender': true};var ps = [{"name": "张三", "age": 23, "gender": true}, {"name": "李四", "age": 24, "gender": true}, {"name": "王五", "age": 25, "gender": false}];
var b='{"name":"2323","sex":"afasdf","age":"6262"}';//json字符串 console.log(b);//{"name":"2323","sex":"afasdf","age":"6262"} alert(typeof(b));//string
The process of converting a data structure or object into a binary string (byte sequence) is used Data transmission (convert the data into a json string and the backend cooperates with @reponseBody to receive and transmit data)
The backend is a Java object. If you want to transmit data in JSON format, you must perform a serialization operation.
Java objects must be serialized before they can be transmitted over the network or saved to the hard disk.
After serialization, it becomes a json string and is serialized through the serialization framework of jackson.
At the backend, add the @reponseBody annotation on the controller to convert the java object obtained from the service layer into json format object, transmitted to the front end
Add @reponseBody before the request parameter of the controller to receive the json format data passed from the front end
This ObjectMapper object is the jackson package Below, this is his dependency
b4b38e33757a6497aa8690936b905cc1 05a8acc5c31084a4f61ade01873802cacom.fasterxml.jackson.core192ca2f7b8c770b01c8f81e6bdd5b947 9bc4cbb67f66e148869423c0d27e5f90jackson-databindb68fb17cb904a46b73e6272850323873 3d689bd3819ead35ed794427bd12f4592.8.383a577b3f930c490b31329be5e672d0b 09a0e22e5aaafd848ae04665be625b91
//类属性, private static final ObjectMapper MAPPER = new ObjectMapper(); //序列化-----userMapList是Listbe1cdaf6779910b92a0a47fc24e82b4f的格式,现在我们需要将List集合序列化为json字符串 MAPPER.writeValueAsString(userMapList); //反序列化---json格式的字符串要反序列化为对象 MAPPER.readValue(你要反序列化的json字符串,new TypeReference5ffa1bc620359d13a5b754cd7e8e7c40() {});
Serialization: Convert objects in Js into Json format, two parameters of serialization: filter and options.
var person = { username: ‘luohao’, password: 123456, location: ‘whu’ }
The parameter is an array. Only the attributes that appear in the array will be serialized, and the other attributes will be ignored.
var json = JSON.stringify(person, [‘username’, ‘password’]); console.log(json); {“username”:“luohao”,“password”:123456}
var person = { username: ‘luohao’, password: 123456, location: ‘whu’, hometown: ‘wuhan’, } var json = JSON.stringify(person, function filter(key, value) { switch(key) { case ‘location’: return undefined; case ‘hometown’: return undefined; default: return value; } }); console.log(json); {“username”:“luohao”,“password”:123456}
The third parameter in JSON.stringify() represents the number of indented spaces, so that the readability of the transmitted data is better.
var person = { username: ‘luohao’, password: 123456, location: { province: ‘hubei’, city: ‘wuhan’, county: ‘qichun’ }, hometown: ‘wuhan’, } var json = JSON.stringify(person, function filter(key, value) { switch(key) { case ‘hometown’: return undefined; default: return value; } }); console.log(json); {“username”:“luohao”,“password”:123456,“location”:{“province”:“hubei”,“city”:“wuhan”,“county”:“qichun”}}rrree
[Related recommendations:javascript video tutorial,web front-end】
The above is the detailed content of Example parsing the serialization and deserialization of Json objects and Json strings. For more information, please follow other related articles on the PHP Chinese website!