- The JSON object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON.
- It can't be called or constructed, and aside from its two method properties it has no interesting functionality of its own.
JavaScript Object Notation (JSON):
- JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and
null
. - It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON, and some JSON is not JavaScript.
JSON = null
or true or false
or JSONNumber
or JSONString
or JSONObject
or JSONArray
JSONNumber = - PositiveNumber
or PositiveNumber
PositiveNumber = DecimalNumber
or DecimalNumber . Digits
or DecimalNumber . Digits ExponentPart
or DecimalNumber ExponentPart
DecimalNumber = 0
or OneToNine Digits
ExponentPart = e Exponent
or E Exponent
Exponent = Digits
or + Digits
or - Digits
Digits = Digit
or Digits Digit
Digit = 0 through 9
OneToNine = 1 through 9
JSONString = ""
or " StringCharacters "
StringCharacters = StringCharacter
or StringCharacters StringCharacter
StringCharacter = any character
except " or \ or U+0000 through U+001F
or EscapeSequence
EscapeSequence = \" or \/ or \\ or \b or \f or \n or \r or \t
or \u HexDigit HexDigit HexDigit HexDigit
HexDigit = 0 through 9
or A through F
or a through f
JSONObject = { }
or { Members }
Members = JSONString : JSON
or Members , JSONString : JSON
JSONArray = [ ]
or [ ArrayElements ]
ArrayElements = JSON
or ArrayElements , JSON
Methods:
1.JSON.parse():
- Parse a string as JSON, optionally transform the produced value and its properties, and return the value.
Syntax: JSON.parse(text[, reviver])
Parameters:
1.Text:
- The string to parse as JSON. See the
JSON
object for a description of JSON syntax. - 2.Reviver:
- If a function, prescribes how the value originally produced by parsing is transformed, before being returned.
- Examples:
- Using JSON.parse()
- 1. JSON.parse('{}'); // {}
- 2. JSON.parse('true'); // true
- 3. JSON.parse('"foo"'); // "foo"
- 4. JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
- 5. JSON.parse('null'); // null
2.JSON.stringify():
- The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
Syntax: JSON.stringify(value[, replacer[, space]])
Parameters:
Value:
- The value to convert to a JSON string.
Replacer:
- A function that alters the behavior of the stringification process, or an array of
String
andNumber
objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string. Space:
- A
String
orNumber
object that's used to insert white space into the output JSON string for readability purposes. If this is aNumber
, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is aString
, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used. JSON.stringify({}); // '{}' 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, 15, 4, 5)) // '"2006-01-02T15:04:05.000Z"' JSON.stringify({ x: 5, y: 6 }); // '{"x":5,"y":6}' or '{"y":6,"x":5}' JSON.stringify([new Number(1), new String('false'), new Boolean(false)]); // '[1,"false",false]' // 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"}'
No comments:
Post a Comment