JSON Object With or Without Quotes

前端 未结 5 383
我寻月下人不归
我寻月下人不归 2020-12-29 12:16

I am trying to learn JSON, i learned that any javascript object with the key in double quotes are considered as JSON object.

And i constructed this object



        
5条回答
  •  半阙折子戏
    2020-12-29 12:44

    JSON.parse() accepts a string and converts to JSON object, it doesnt take a javascript object as the parameter. Refer JSON.parse() It could give you the results as follows

    JSON.parse('{}'); // {}
    JSON.parse('true'); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse('null'); // null
    

    and do know that If the string to parse is not valid JSON, a SyntaxError exception is thrown. so this is how you get syntax error on jstr1 (It is not a JSON string)

提交回复
热议问题