This article brings you relevant knowledge about javascript, 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
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency>
//类属性, private static final ObjectMapper MAPPER = new ObjectMapper(); //序列化-----userMapList是List<user>的格式,现在我们需要将List集合序列化为json字符串 MAPPER.writeValueAsString(userMapList); //反序列化---json格式的字符串要反序列化为对象 MAPPER.readValue(你要反序列化的json字符串,new TypeReference<要反序列化为什么对象类型,例如 User.class>() {});
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”}}
[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!