Show:

Json Class

Module: json
  • Index
  • Methods

Item Index

Methods

Methods

parse

(
  • text
  • [reviver]
)
Object static

将字符串解析为json对象

Parameters:

  • text String

    字符串

  • [reviver] Object optional

    过滤器,可选

Returns:

Object:

解析之后返回传入数据的一个对象表示

Example:

注意 json 字符串的格式,属性必须要双引号括起来

require(['json'],function(JSON){
    JSON.parse('{"x":1}'); // => ok
    JSON.parse("{'x':1}"); // => exception : SyntaxError
    JSON.parse("{x:1}"); // => exception : SyntaxError
})

stringify

(
  • value
  • [replacer]
  • [space]
)
String static

将json对象或者数组转化为字符串,序列化器

Parameters:

Returns:

String:

返回JSON字符串

Example:

JSON.stringify({"x":1}); // => '{"x":1}'
JSON.stringify({x:1}); // => '{"x":1}'
JSON.stringify({'x':1}); // => '{"x":1}'