Using native JSON

Source: Internet
Author: User
Tags string to json

A native JSON object compatible with the ECMAScript 5 standard is described . How to work with JSON data in older versions of Firefox that do not support native JSON objects. See json.

Native JSON objects contain two key methods. JSON.parse()方法用来解析 JSON string, and is refactored into a JavaScript object, andJSON.stringify()方法可以将javascript对象转换为等价的JSON字符串.

Note:Because of the limitations of the JSON specification, if an object exists with a property value of a function, the object is serialized using the Json.stringify () method. This property will not be traversed to. Parse JSON string

parsing the JSON string into a JavaScript object, you just need to pass the JSON string as a parameter to json.parse () method is as follows:

var jsObject = JSON.parse(jsonString);

JavaScript 1.8.5 Notes

Introduced in JavaScript 1.8.5 (Firefox 4), JSON.parse() does not allow the last key value pair to be followed by a comma

// JavaScript 1.8.5中,下述两行代码都会抛出SyntaxError异常var jsObject = JSON.parse("[1, 2, 3, 4, ]");var jsObject = JSON.parse("{ \"foo\" : 1, }");
Convert an object to JSON

to convert a JavaScript object string to JSON, you just need to pass the object as a parameter to json.stringify ( ) method, as follows:

var foo = {}; foo. Bar = "New property"; Foo. Baz = 3;  var jsonstring = JSON.  Stringify(foo);                

jsonstring   a new value of ' {' bar ': ' New property ', ' Baz ': 3} ' .

 

from Firefox 3.5.4 onwards,  json.stringify () provides an optional parameter to provide an additional ability to customize the conversion method. As follows:

jsonstring = json.stringify ( value  [,  replacer  [,  space ])

value
the JavaScript object that will be converted to a JSON string.
replacer
The argument can be of multiple types, and if it is a function, it can change the behavior of a JavaScript object during the string, if it is an array of inclusions String and Number objects, Then it will be a whitelist. Only those keys exist in the domain the key-value pairs in the whitelist are included in the resulting JSON string. If the parameter value is NULL or omitted, all key-value pairs are included in the resulting JSON string.
space
This parameter can be a string or Number object that is used to insert white space characters in the output JSON string to enhance readability. If the Number object, it is the number of spaces to use as whitespace, the maximum can be 10, the value greater than 10 is also taken 10. The minimum value is 1, and the number less than 1 is invalid , the white space character is not displayed. If it is String对象 , the string itself is used as a white-space character, and the string can be up to 10 characters long. The first 10 characters will be truncated if the argument is omitted (or null), the white space character is not displayed.
Replacer parameters

replacer parameter can be a function or an array. If it is a function, the function is passed two arguments, respectively, the key and value of the key-value pair that is currently being serialized. At initialization time, a default key-value pair (the key is null, the value is the object to be JSON) is passed into the function, and then each key-value pair of the object or array that is being JSON is also passed to this function at the first level. The return value of the function affects whether the key-value pair being serialized will be added to the resulting JSON string, as follows:

  • If a value of a type is returned Number , the number is converted to a string by the ToString method, which is added to the JSON string as the value in the key-value pair being serialized.
  • If a value of a type is returned String , the string is added to the JSON string as the value in the key-value pair being serialized.
  • If a value of a type is returned Boolean , the Boolean value is converted to the string "true" or "false" to be added to the JSON string as the value in the key-value pair being serialized.
  • If an object of another type is returned, the object's key-value pair is recursively added to the JSON string as described above, but it does not traverse replacer函数会 if the object is of type function. The next key-value pair is processed instead.
  • If returned undefined , the key-value pair is not added to the JSON string.
Note:You cannot replacer reject certain values in an array by using a function. Example
functionCensor(Key, value){If(typeof(Value)=="String"){return undefined;}return value;}var foo={Foundation:"Mozilla", model: "box", week: transport: "Car", month: 7 }; var jsonstring = JSON.  Stringify(foo, censor);             

jsonstring   a new value of {"Week": "Month": 7} .

In the case of replacer An array, only key- replacer value pairs in the array whose keys exist in the source object are included into the final JSON string.

RELATED LINKS
    • ECMAScript 5 support in Mozilla
    • Json

Using native 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.