JSON serialization



JSON serialization


JSON is a lightweight data format, which can simplify the workload of representing complex data structures,It does not support variables, functions or object instances. JSON strings must use double quotation marks (single quotation marks will lead to syntax errors); When writing JSON manually, attribute names in JSON objects must always be enclosed in double quotes。 JSON is an object defined by ecmascript5. It has two methods: son Stringify() and JSON parse()。


function: serialize JavaScript objects into JSON strings (convert JavaScript objects into JSON data transfer)

grammar:JSON.stringify(value[, replacer [, space]]) => JSON.stringify(value, replacer , space)

  • value: the value to be serialized as a JSON string

  • replacerOptionalFilter parameters. (parameters may be array, function or null) = > matching rules for conversion

    • Array: only the attribute names contained in this array will be displayed;

    • Function: the incoming function receives two parameters, attribute name and attribute value;

    • If / null is not provided, it will not be filtered and all properties will be displayed;

  • space:OptionalControls indentation and whitespace in results. Used to beautify output. (the parameter may be numeric, string, not provided, or null)

    • Number: on line is 10, if it is greater than 10, it will be converted to 10, and if it is less than 1, there will be no space;

    • String: the longest string length shall not exceed 10. If it is greater than 10, the first 10 will be taken and displayed instead of spaces;

    • No / null provided: no space;

Use JSON stringify()

JSON.stringify({});                        // '{}'
JSON.stringify(true);                      // 'true'
JSON.stringify("foo");                     // '"foo"'
JSON.stringify([1, "false", false]);       // '[1,"false",false]'
JSON.stringify({ x: 5 });                  // '{"x":5}'
JSON.stringify({x: 5, y: 6});              // "{"x":5,"y":6}"

The replacer parameter is a function

//JSON.stringify(value[, replacer [, space]])
function replacer(key, value) {
  if (typeof value === "string") {
    return undefined;
  return value;

var foo = 
{foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};

JSON.stringify(foo, replacer);
JSON serialization


The replacer parameter is an array

//JSON.stringify(value[, replacer [, space]])
var obj = 
{"age": 19, "name": "Qianjun", "functionname": "test", "address": "China"};

JSON. stringify(obj, ['age', 'name']); //  "{" age ": 19," name ":" Qianjun "}"
JSON serialization


Space parameter

//JSON.stringify(value[, replacer [, space]])
var obj = 
{"age": 19, "name": "Module", "functionName": "test", "address": "china"};

JSON.stringify(obj, null, 2)

JSON serialization



function: parse JSON string into native JavaScript object (convert JSON data into JavaScript object)

grammar:JSON.parse(text[, reviver]) =>JSON.parse(text, reviver)

  • Text: the string to be parsed as a JavaScript object

  • reviver:OptionalFunction, which will be called on each key value pair to modify the original value generated by parsing, and called before parse returns. (for data)

be careful: thrown if the incoming string does not conform to the JSON specificationSyntaxErrorAbnormal.

Use JSON parse()

JSON.parse('{}');              // {}
JSON.parse('true');            // true
JSON.parse('"foo"');           // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');            // null

Using the Reviver function

JSON.parse('{"p": 5}', function (k, v) {
    if(k === '') return v;     //  If it reaches the top level, the property value will be returned directly,
    return v * 2;              //  Otherwise, the attribute value will be doubled.

JSON. Parse() does not allow comma terminated parsing of the original JavaScript object


Errors are reported as follows:

JSON serialization