這么說可能好點(diǎn),實(shí)際上存在JSON字符串和JSON對(duì)象,而這兩個(gè)方法便是用于兩者之間的相互轉(zhuǎn)化的
JSON.parse()
從JSON字符串中提取JSON
var str = '{"name":"Elysion","age":"12"}'
undefined /*控制臺(tái)返回值,由于直接在瀏覽器調(diào)試,所以會(huì)輸出該值*/
JSON.parse(str)
Object {name: "Elysion", age: "12"} //控制臺(tái)輸出
如果字符串并非規(guī)則的JSON字符串,會(huì)返回錯(cuò)誤,包括錯(cuò)誤位置
var str = '{"name":"Elysion","age":"12"}, What is Nein?'
JSON.parse(str)
/*控制臺(tái)輸出*/
Uncaught SyntaxError: Unexpected token , in JSON at position 29
at JSON.parse (<anonymous>)
at <anonymous>:1:6
JSON.stringify()
從JSON對(duì)象提取成JSON字符串
var Sound = {moria:6,Elysion:4}
undefined
JSON.stringify(Sound)
"{"moria":6,"Elysion":4}"http://控制臺(tái)輸出