JSON.parse() 和JSON.stringify()
JSON.parse() 和JSON.stringify()
1、parse 用于从一个字符串中解析出json 对象。例如
var str='{"name":"aaa","age":"23"}' 经 JSON.parse(str) 得到:
Object
age:"23"
name:"aaa"
_proto_:Object
ps:单引号写在{}外,每个属性都必须双引号,否则会抛出异常
![](https://img.haomeiwen.com/i2025073/be60fbf147a90ba7.png)
官网解释:
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).
![](https://img.haomeiwen.com/i2025073/27db1d6ceec06c6f.gif)
Anarrayis an ordered collection of values. An array begins with[(left bracket)and ends with](right bracket). Values are separated by,(comma).
![](https://img.haomeiwen.com/i2025073/5249d05137ef892a.gif)
Avaluecan be astringin double quotes, or anumber, ortrueorfalseornull, or anobjector anarray. These structures can be nested.
![](https://img.haomeiwen.com/i2025073/87cc86391c321bca.gif)
2、stringify用于从一个对象解析出字符串,例如
var a={a:1,b:2}
经 JSON.stringify(a)得到:
“{“a”:1,"b":2}”