JSON Object With or Without Quotes

前端 未结 5 377
我寻月下人不归
我寻月下人不归 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)

    0 讨论(0)
  • 2020-12-29 12:46

    You have some missunnderstanting for JSON.parse

    JSON.parse takes string and parse it to JAVASCRIPT object
    JSON.stringify takes an object and parse it to a string
    

    thats why when you ran the following

    JSON.parse('{"a":"b"}')
    

    it worked because it expects a json string

    but when you ran

    JSON.parse({"a":"b"})
    

    it didnt because the object was coverted to string which is

    "[object Object]"
    

    and here is the error where "[object Object]" is not valid syntax at letter o

    0 讨论(0)
  • 2020-12-29 12:49

    You are creating a Javascript Object. If you want a JSON-string from it, use JSON.stringify.

    So

    var myObj = {mykey: "my value"}
       ,myObjJSON = JSON.stringify(myObj);
    

    Based on comments: There is no such thing as a JSON Object. There are JSON-strings, which can be parsed to Javascript Objects. Javascript Objects can be stringified to JSON strings. Within a JSON-string keys and values are quoted. So the result of the above is a string containing '{"mykey":"my value"}'.

    Try parsing myObjJSON in your browser console (using: JSON.parse(myObjJSON)) and you get: Object {mykey: "my value"}.

    0 讨论(0)
  • 2020-12-29 12:54

    This code

    var jstr1 = {"mykey": "my value"};
    

    creates a JavaScript object using the Object Literal Notation.
    For the difference between the Object Literal Notation and JSON (JON is short for JavaScript object notation), see here: What is the difference between JSON and Object Literal Notation?

    It makes logically no sense to pass this data to JSON.parse().

    The difference to your first variant (var jstr = '{"mykey": "my value"}';) is that it creates a 'raw' string. You cannot access anything on that string except the raw character sequences. Using JSON.parse() gives us a usable form (object) created from the string.

    SyntaxError: Unexpected token o

    This comes from the automatic string conversion of jstr1:

    jstr1.toString();
    // gives us [object Object]
    // ----------↑
    
    0 讨论(0)
  • 2020-12-29 12:54

    How about this:

    MarahJSONObject gtp = new MarahJSONObject()
        gtp.put("ecomm_prodid", "123")
        gtp.put("ecomm_pagetype", "cart")
        gtp.put("ecomm_totalvalue", "19.99")
    
    String r = gtp.toString()
        gtp.keySet().each {
            r = r.replace(/"${it}"/, it)
        }
        println r
    

    then you will get: {ecomm_pagetype:"cart",ecomm_prodid:"123",ecomm_totalvalue:"19.99"}

    0 讨论(0)
提交回复
热议问题