JSON.parse() 和JSON.stringify()
1、parse 用于从一个字符串中解析出json 对象。例如
var str='{"name":"aaa","age":"23"}' 经 JSON.parse(str) 得到:
Object
age:"23"
name:"aaa"
_proto_:Object
ps:单引号写在{}外,每个属性都必须双引号,否则会抛出异常
官网解释:
In JSON, they take on these forms:
Anobjectis an unordered set of name/value pairs. An object begins with{(left brace)and ends with}(right brace). Each name is followed by:(colon)and the name/value pairs are separated by,(comma).
Anarrayis an ordered collection of values. An array begins with[(left bracket)and ends with](right bracket). Values are separated by,(comma).
Avaluecan be astringin double quotes, or anumber, ortrueorfalseornull, or anobjector anarray. These structures can be nested.
2、stringify用于从一个对象解析出字符串,例如
var a={a:1,b:2}
经 JSON.stringify(a)得到:
“{“a”:1,"b":2}”