JavaScript JSON format operation

Source: Internet
Author: User

The most important thing about JSON is to understand that it is a data format, not a programming language. Although it has the same syntax, JSON does not belong to JavaScript. Also, JSON is not only used by JavaScript, after all, JSON is just a data format. Many programming languages have parsers and serializers for JSON.

First, the JSON syntax, JSON consists of three types, simple, object, array. Simple values include numeric value, String, Boolean, null. An object consists of a set of key-value pairs that can nest arrays and objects. An array represents an ordered set of worthwhile lists that can contain arrays and objects.

Console.log (Json.parse (1))

This is not wrong to write, can directly parse the value, but this does not make sense, the general application of the JSON is in the form of objects or arrays presented. Here's a look at the object's notation:

{"Name": "Miracle", "Age": 24} ["SD"]

JSON objects and arrays are similar to JavaScript object literals and array literals, and all keys in the JSON are wrapped in double quotes, otherwise they can cause syntax errors. The value of the string must also be wrapped in double quotes, not single quotes, because it can cause syntax errors, as well as in arrays. In JSON, an object can contain an array, and the same array can contain objects.

{"Name": "Miracle", "age": +, "skills": ["AJAX", "ES5", "HTML5", "CSS3"], "friends": [{"Name": "Kirs", "Age": 23},{"nam E ":" HQ "," Age ": 22}]}

The most peripheral of the above code is an object that contains 2 simple values, and two arrays, and the second array "Friends" contains two objects.

  

Parsing and serialization

Before the JSON object was born, the parsing of JSON has been to use the Eval () function, and the evaluation of JSON data structures using eval () is risky because some malicious code might be executed. ECMAScript 5 describes the behavior of parsing JSON, and defines the global object json. Browsers that support this object are ie8+, Firefox 3.5+, Safari 4+, Chrome, and opera 10.5+.

There are two methods of JSON objects: Stringify () and parse (). Stringify () is used to convert an instance of a JSON object to a JSON string, while json.parse () resolves a json string to a JSON object.

var jobj = {"Name": "Miracle", "Age": 24}var Jtext = json.stringify (jobj); Console.log (jtext);/' {"name": "Miracle", "Age" : Console.log (typeof jtext),//stringvar obj = Json.parse (jtext); Console.log (obj.name);//"Miracle"

When executing the json.stringify () method, the current key-value pair is ignored for special values in JS, such as the undefined,function type.

var jobj = {"Name": "Miracle", "Age": "un": Undefined, "getName": function () {alert (0)}} Console.log (Json.stringify (jobj)); ' {' name ': ' Miracle ', ' Age ': 24} '

The Jobj object in the code above contains a key of "un", and its value is undefined because JSON does not recognize this basic type, it ignores all keys and values. JS in the Funciton is also.

The Json.stringify () method has three parameters, the first parameter is the object to be converted, this parameter is a required option, the second parameter is a filter setting, can be an array, or it can be a function. The third parameter is a formatted setting, which can be a string or a numeric value. Note that the filtering and formatting here are all actions on the object.

var jobj = {"Name": "Miracle", "age": +, "siklls": ["AJAX", "ES5", "HTML5", "CSS3"]}console.log (Json.stringify (Jobj, ["N Ame "])); ' {' name ': ' Miracle '} '

If the second argument of the Json.stringify () method is an array, then the values in the array are matched in the same way as the name of each key in the execution, and if there is a return to this key value pair, it is ignored. If none of them eventually returns an empty object string "{}".

var jobj = {"Name": "Miracle", "Age": $, "siklls": ["AJAX", "ES5", "HTML5", "CSS3"]}var s = json.stringify (Jobj, Functio N (Key, Val) {if (key = = "Name") {return ' Who is ' you ';} else if (key = = "Siklls") {return undefined;} else {return val;}}); Console.log (s);//{"name": "Who is?", "Age": 24}

If the json.stringify () second argument is a function, match it according to the value returned by the function, and if the function does not return from start to finish, then the result is undefined. This function takes two parameters, the current key and the value, and then returns the current key and the changed value based on the result. For example, the result in the else if judgment in the code returns the undefined, which says that if the data returned is not JSON-type, the JSON is automatically filtered.

The last parameter of the Json.stringify () method is used to format the string. The change parameter is a string or numeric type. If it is a numeric value, it is indented using the number of spaces in the current numeric value, and if the string is used to indent the string, note that the line will be wrapped in this case.

var jobj = {"Name": "Miracle", "Age": $, "siklls": ["AJAX", "ES5", "HTML5", "CSS3"]}var s = json.stringify (Jobj, NULL, 4 ); Console.log (s);

 

Is the result that is viewed using the CHROMG console.

 

JavaScript JSON format operation

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.