JSON represents a data format that can be used to represent common types (numeric words/strings/booleans), objects, arrays, null, and is much more compact and convenient than XML.

We usually serialize objects to JSON strings using json.stringify, and parse JSON data to JS objects using json.parse. Here is a simple demo.

const zhaji = {
  name: "zhaji".  age: "22".  family: ["mom"."dad"]
};
 const zhajistr = JSON.stringify(zhaji); // "{"name":"zhaji","age":"22","family":["mom","dad"]}"  const zhajicopy = JSON.parse(zhajistr); // Similar to zhaji objects Copy the code

JSON.stringify(val, ? replacer, ? space)

In fact, json.stringify can take three arguments, value(js object),? Replacer,? Space (indentation). (? Indicates optional)

Replacer **** filters can be arrays or functions.

  • The array entry represents the key value to return
JSON.stringify(zhaji, ["name"."family"]);
// "{"name":"zhaji","family":["mom","dad"]}"
Copy the code
  • The function takes two arguments, key value, and returns the corresponding key value. By default, value is returned
JSON.stringify(zhaji, (key, val) => {
  switch(key){
    case "name": return val;
    case "age": return 23;
    case "family": {
 // Note that this will affect the family of the zhaji object because it points to the same address  val.push("sister");  return val;  }  default: return val;  } }) // "{"name":"zhaji","age":23,"family":["mom","dad","sister"]}" Copy the code

The space indent **** can be a number or a character

  • The number indicates how many Spaces are indented
JSON.stringify(zhaji, ' '.4)
/ *"{
    "name": "zhaji",
 "age": "22",  "family": [  "mom",  "dad"  ] }" * / Copy the code
  • Character representation Indicates indentation with this character
JSON.stringify(zhaji, ' '.The '-')
/ *"{
--"name": "zhaji",
--"age": "22", --"family": [ ----"mom", ----"dad" -]}" * / Copy the code

toJSON

The toJSON method allows developers to customize the behavior of serialization. Can be used as a supplement to filters.

const zhaji = {
  name: "zhaji".  age: "22".  family: ["mom"."dad"].  toJSON(){
 return "hi zhaji!"  } };  JSON.stringify(zhaji) // ""hi zhaji!" " Copy the code

Serialized objects are in the following order ^[1]^

  1. If there istoJSONMethod is called and returns the return value of the method, otherwise the object itself is returned
  2. If a filter exists, the value passed in is the return value of the first step
  3. Serialize each value of the second step
  4. If the corresponding object exists, perform the corresponding formatting

JSON.parse(value, ? reviver)

Similarly, json. parse also takes two arguments, value(JSON string) and reviver(reduction function). Like the filter above, this function also takes two key/value arguments, but in different circumstances, so it’s called differently.

JSON.parse(zhajistr, (key, val) => {
      switch(key){
        case "name": return `${val}ya! `;
        case "age": return 18;
        case "family": {
 val.push("sister");  return val;  }  default: return val;  } }) / *{ name: "zhajiya!" . age: 18,  family: (3) ["mom", "dad", "sister"] } / *Copy the code

Values that will not be serialized

Not all values are serialized, and key-value pairs are ignored in the following cases

  • The value value ofundefined
  • Value The value cannot be enumerated
const noappear = {
    hi1: undefined
}
Object.defineProperties(noappear,{
  hi2: {
 value: "hi2". enumerable: false  },  hi3: {  value: "hi3". enumerable: true  } })  JSON.stringify(noappear) // "{"hi3":"hi3"}" Copy the code

References:

[1] “JavaScript Advanced Programming, 3rd edition, P569.

[2] JSON | MDN https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/JSON


Welcome to follow my other accounts ~

  • Blog: blogging. Escript. Cn /

  • Nuggets: The pig reads backwards

  • The pig reads backwards

  • Public account: Fried chicken (more)