json--Eon Gray JS notes

Source: Internet
Author: User
Tags tojson

//simple values for JSON10NULLtrue"FC"//object representsvarbox ={name:' FC ', Age:25};//The JSON object represents{    "Name": "FC",    "Age": "25"}' {' name ': ' FC ', ' Age ': ' 25 '} '//Ps:json is plainly a string, so any representation should be quoted as a string. //Normal Arrayvarbox = [+, ' FC ',true];//JSON Array' [+, ' FC ', True] '//Ps:json objects and arrays are smaller than normal objects and arrays, with fewer semicolons, less variable assignments, and themselves as string representations. //The most commonly used JSON structure[    {        "Name": "FC",        "Age": 25    },    {        "Name": "LQ",        "Age": 25    },    {        "Name": "Hehe",        "Age": 25    }]//simulates the process of loading a JSON data string, var json = load (' Demo.json ');//load the JSON string in and assign it to the JSON variable. varJSON = [{"title": "A", "num": 1},{"title": "B", "num": 2}];varJson1 = ' [{' title ': ' A ', ' num ': 1},{' title ': ' B ', ' num ': 2}] '; Console.log (JSON); //[Object, Object]Console.log (Json1);//[{"title": "A", "num": 1},{"title": "B", "num": 2}]Console.log (typeofJson1);//string//parsing and serialization//if it is a loaded JSON file that we need to use, then we must parse the JSON string into native JavaScript values. Of course, if it is a native JavaScript object or array, it can also be converted to a JSON string. //The eval () function was used earlier for parsing a JSON string into a JavaScript native value. However, this approach is not secure and may execute some malicious code. varJSON = ' [{' title ': ' A ', ' num ': 1},{' title ': ' B ', ' num ': 2}] ';varbox =eval (JSON); Console.log (box); //[Object, Object]Console.log (Box[1].title);//bvarbox =[{title:A, Num:1, Height:122}, {title:' B ', Num:2, Height:133    }]varJSON =json.stringify (box); Console.log (JSON); //[{"title": "A", "num": 1, "height": 122},{"title": "B", "num": 2, "height": 133}]varbox =[{title:A, Num:1, Height:122}, {title:' B ', Num:2, Height:133    }]varJSON = json.stringify (box,[' num ', ' height ']); Console.log (JSON); //[{"num": 1, "height": 122},{"num": 2, "height": 133}]varbox =[{title:A, Num:1, Height:122}, {title:' B ', Num:2, Height:133    }]varJSON = json.stringify (box,function(key, value) {if(key = = ' title '){        return' Mr. ' +value; } Else {        returnvalue;  }}); Console.log (JSON); //[{"title": "MR.A", "num": 1, "height": 122},{"title": "Mr.b", "num": 2, "height": 133}]//PS: Firefox 3.5 and 3.6 when initially supporting JSON stringify method has a small bug, the execution of funxtion error. varbox =[{title:A, Num:1, Height:122}, {title:' B ', Num:2, Height:133    }]varJSON = json.stringify (box,[' title ', ' Num '), 4);varJson2 = json.stringify (box,[' title ', ' num '), '--'); Console.log (JSON);// [//     {//" title": "A",//"num": 1//     },//     {//" title": "B",//"num": 2//     }// ]Console.log (json2);// [// --{//----"title": "A",//----"num": 1// --},// --{//----"title": "B",//----"num": 2// --}// ]varbox =[{title:A, Num:1, Height:122}, {title:' B ', Num:2, Height:133    }]varJSON = json.stringify (box,NULL, 4); Console.log (JSON);// [//     {//" title": "A",//"num": 1,//"height": 122//     },//     {//" title": "B",//"num": 2,//"height": 133//     }// ]varbox =[{title:A, Num:1, Height:122, ToJSON:function () {            return  This. title; }}, {title:' B ', Num:2, Height:133, ToJSON:function () {            return  This. title; }    }]varJSON =json.stringify (box); Console.log (JSON); //["A", "B"]//JSON serialization also has an execution order, first executing the Tojson () method, or if the second parameter is applied. The serialization process is then performed. For example, the key-value pairs make up a valid JSON string, such as a double quotation mark, if the indentation is provided, the indentation is performed. varJSON = ' [{' title ': ' A ', ' num ': 1},{' title ': ' B ', ' num ': 2}] ';varbox =Json.parse (JSON) Console.log (JSON);

json--Eon Gray JS notes

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.