带或不带引号的 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18635132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON Object With or Without Quotes
提问by Mohamed Hussain
I am trying to learn JSON, i learned that any javascript object with the key in double quotes are considered as JSON object.
我正在尝试学习 JSON,我了解到任何带有双引号键的 javascript 对象都被视为 JSON 对象。
And i constructed this object
我构造了这个对象
var jstr1 = {"mykey": "my value"};
But when i try to parse using JSON.parse(jstr1), i got the following error. see the screenshot.
但是当我尝试使用 JSON.parse(jstr1) 进行解析时,出现以下错误。看截图。


But when i try to parse this
但是当我尝试解析这个时
var jstr = '{"mykey": "my value"}';,
i got the success, see the screenshot. i got confused with this. Please explain me why this happens. what is the difference between the two forms.
我成功了,看截图。我对此感到困惑。请解释为什么会发生这种情况。这两种形式有什么区别。
And when i got JSON as a response from any services, how it would look like, whether it will be in form of jstror jstr1
当我从任何服务收到 JSON 作为响应时,它会是什么样子,无论是形式jstr还是jstr1
thanks in advance for any help.
提前感谢您的帮助。
回答by KooiInc
You are creating a Javascript Object. If you want a JSON-string from it, use JSON.stringify.
您正在创建一个 Javascript Object。如果您想从中获取 JSON 字符串,请使用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 parsedto Javascript Objects. Javascript Objects can be stringifiedto JSON strings. Within a JSON-string keys and values are quoted. So the result of the above is a stringcontaining '{"mykey":"my value"}'.
基于评论:没有JSON Object这样的东西。有 JSON 字符串,可以是parsedJavascript 对象。Javascript 对象可以是stringifiedJSON 字符串。在 JSON 字符串中,键和值被引用。所以上述结果是一个字符串包含'{"mykey":"my value"}'。
Try parsing myObjJSONin your browser console (using: JSON.parse(myObjJSON)) and you get: Object {mykey: "my value"}.
尝试myObjJSON在浏览器控制台中解析(使用 : JSON.parse(myObjJSON)),您会得到:Object {mykey: "my value"}.
回答by ComFreek
This code
这段代码
var jstr1 = {"mykey": "my value"};
creates a JavaScript object using the Object Literal Notation.
For the difference between the Object Literal Notationand JSON(JON is short for JavaScript object notation), see here: What is the difference between JSON and Object Literal Notation?
使用Object Literal Notation创建一个 JavaScript 对象。
关于Object Literal Notation和JSON(JON 是JavaScript object notation 的缩写)的区别,请看这里:JSON 和 Object Literal Notation 有什么区别?
It makes logically no sense to pass this data to JSON.parse().
将此数据传递给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.
与您的第一个变体 ( var jstr = '{"mykey": "my value"}';)的不同之处在于它创建了一个“原始”字符串。除了原始字符序列之外,您无法访问该字符串上的任何内容。UsingJSON.parse()为我们提供了一个从字符串创建的可用表单(对象)。
SyntaxError: Unexpected token o
语法错误:意外标记 o
This comes from the automatic string conversion of jstr1:
这来自 的自动字符串转换jstr1:
jstr1.toString();
// gives us [object Object]
// ----------↑
回答by Hilmi
You have some missunnderstanting for JSON.parse
你有一些误解 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
它有效,因为它需要一个 json 字符串
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
这是“[object Object]”在字母处无效语法的错误 o
回答by Ayyappan Sekar
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 对象,它不以 javascript 对象作为参数。参考JSON.parse()可以得到如下结果
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)
并且知道如果要解析的字符串不是有效的 JSON,则会引发 SyntaxError 异常。所以这就是你在 jstr1 上得到语法错误的方式(它不是一个 JSON 字符串)
回答by user3832462
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"}
然后你会得到:{ecomm_pagetype:"cart",ecomm_prodid:"123",ecomm_totalvalue:"19.99"}

