JavaScript objects vs. json

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.