Home > Web Front-end > JS Tutorial > Example parsing the serialization and deserialization of Json objects and Json strings

Example parsing the serialization and deserialization of Json objects and Json strings

WBOY
Release: 2022-08-08 15:40:34
forward
2217 people have browsed it

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.

Example parsing the serialization and deserialization of Json objects and Json strings

[Related recommendations: javascript video tutorial, web front-end

Json

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}];
Copy after login

Json string

var b='{"name":"2323","sex":"afasdf","age":"6262"}';//json字符串
 console.log(b);//{"name":"2323","sex":"afasdf","age":"6262"}
  alert(typeof(b));//string
Copy after login

Serialization

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 front-end has json objects and json strings
  • The backend has objects and json strings

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

Serialization and deserialization of the front end

  • Serialize the json object into a json string: JSON.stringify (json object) – the front end serializes the data and passes it to the back end
  • Deserialization: Deserialize the json string into an object : JSON.parse(str) – Serialized data passed from the backend

Serialization and deserialization of the backend

  • Serialization is a java object- >json string.
  • Deserialization is json string->java object

Backend serialization and deserialization method one

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>
Copy after login
//类属性,

private static final ObjectMapper MAPPER = new ObjectMapper();

//序列化-----userMapList是List<user>的格式,现在我们需要将List集合序列化为json字符串

MAPPER.writeValueAsString(userMapList);

//反序列化---json格式的字符串要反序列化为对象

MAPPER.readValue(你要反序列化的json字符串,new TypeReference<要反序列化为什么对象类型,例如 User.class>() {});
Copy after login

Serialization: Convert objects in Js into Json format, two parameters of serialization: filter and options.

var person = {
username: ‘luohao’,
password: 123456,
location: ‘whu’
}
Copy after login

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}
Copy after login
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}
Copy after login

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”}}
Copy after login
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!

Related labels:
es6
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template