Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the advanced use of string serialization and deserialization in json format

Shulou Source: shulou.com Published: 2022-06-01 13:52:52 09月22日 Update

Json format string serialization and deserialization of what is the advanced use, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can get something.

(1) only serialize the value corresponding to the specified attribute into a string:

Var filter = {name: 'Jerry', sex:' male', age: '10'}; var jsonStr4 = JSON.stringify (filter, [' name']); console.log (jsonStr4)

The name of the parameter you want to participate in the serialization is passed in through the second parameter of the JSON.stringify method.

In the above example, only the name field and its corresponding values participate in the serialization, and the output is as follows:

(2) when serializing, you can also write functions to implement custom serialization logic:

Var obj = {name: 'Jerry', sex:' male', age: '40'}; var jsonStr5 = JSON.stringify (obj, function (key, value) {if (key =' name') {return'my name is:'+ value;} else if (key = 'sex') {/ do not return, discard this attribute} else return value;})

Output:

The value of the name attribute is implemented through a custom function prefixed with a prefix. The sex property is discarded and the age property remains unchanged.

(3) add indentation when serializing to make the output string easier to read.

Var obj6 = {name: 'Jerry', sex:' male', age: '40'}; var jsonStr6 = JSON.stringify (obj6, null, 5); console.log (jsonStr6)

Output:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Sequence attribute output character string function parameter help advanced format clear example content prefix only field article novice method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker OPPO Reno Huawei MySQL