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:單引號寫在{}外,每個屬性都必須雙引號,否則會拋出異常


官網解釋:

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}”



最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容