JSON.parse() 和JSON.stringify()
1型宝、parse 用于從一個(gè)字符串中解析出json 對象木人。例如
var str='{"name":"aaa","age":"23"}'? 經(jīng) JSON.parse(str) 得到:
Object
age:"23"
name:"aaa"
_proto_:Object
ps:單引號寫在{}外便监,每個(gè)屬性都必須雙引號勾栗,否則會拋出異常
官網(wǎng)解釋:
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用于從一個(gè)對象解析出字符串凤薛,例如
var a={a:1,b:2}
經(jīng) JSON.stringify(a)得到:
“{“a”:1,"b":2}”