JSON has a very strict syntax, in the string context {"prop": "Val"} is a valid JSON, but {prop: "Val"} and {' prop ': ' Val '} is indeed illegal. All property names and their values must be enclosed in double quotation marks and cannot be used in single quotes. using a JavaScript object with curly braces in a string context is a JSON string, and if used in the context of an object literal, it is the object literal .
Json.stringify (TRUE); //' true '
Json.stringify (' foo '); //' "foo" '
Json.stringify ([1, ' false ', false]); //' [1, "false", false] '
Json.stringify ({ x: 5 }); ' {' X ': 5} '
Json.stringify (New Date (2006, 0, 2, 4, 5)) //' "2006-01-02t15:04:05.000z"
Json.stringify ({ x: 5, y: 6 });//' {"X": 5, "Y": 6} ' or ' {"Y": 6, "X": 5} ' /c10>
Json.stringify ([New number (1), new String (' false '), new Boolean (false)]),//' [1, ' false ', FALSE] '
Json.stringify ({ x: [Ten, Undefined, function () {}, Symbol (')] }); //' {"x": [10,null,null, NULL]} '
Symbols:
Json.stringify ({ x: undefined, y: Object, z: Symbol (') });//' {} '
Json.stringify ({ [' Symbol (' foo ')]: ' foo ' });//' {} '
Json.stringify ({ [symbol.for (' foo ')]: ' foo ' }, [symbol.for (' foo ')]);//' {} '
Json.stringify ({ [symbol.for (' foo ')]: ' foo ' }, function (K, v) {
If (typeof k = = = ' symbol ') {
Return ' a symbol ';
}});// ‘{}‘
Non-enumerable Properties:
Json.stringify ( object.create (null, { x: { value: ' x ', enumerable: false }, y: { value: ' y ', enumerable: True } }) ;//' {"Y": "Y"} '
function replacer (key, value) {
Filtering out Properties
If (typeof value = = = "string") {
return undefined;
}
return value;}
var foo = {foundation: "Mozilla", Model: "box", Week: Transport: "Car", Month: 7};
Json.stringify (foo, replacer);//' {"Week": "Month": 7} '
Json.stringify (foo, [' Week ', ' month ']); ' {' Week ': ', ' Month ': 7} ', only keep ' Week ' and ' Month ' properties
If a string object has a property called Tojson whose value is a function, then the Tojson () method customizes the behavior of the JSON string, rather than the serialized object, the returned value is serialized when the Tojson () method is called. For example
var obj = {
Foo: ' foo ',
ToJSON: function () {
return ' bar ';
}};
Json.stringify (obj); ' Bar '
Json.stringify ({ x: obj}); //' {"X": "Bar"} '
5:
Json.parse ()
Json.parse (' {} '); // {}
Json.parse (' true '); //True
Json.parse (' "foo"); //"foo"
Json.parse (' [1, 5, ' false '] '); //[1, 5, "false"]
Json.parse (' null '); //null
JavaScript objects vs. json