I have a javascript object called blocki
that I want to serialize and update using a rest API. So I call:
我有一个名为blocki的javascript对象,我想使用rest API进行序列化和更新。所以我打电话给:
JSON.stringify(blocki)
And that gives me this string:
这给了我这个字符串:
"{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}"
That is almost what I need, except the doubly quoted string should have single quotes on the outside, like so:
这几乎是我所需要的,除了双引号字符串外面应该有单引号,如下所示:
'{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}'
According to examples on MDN JSON.stringify it should return an string wrapped in single quotes. But when I try the same example in that page, I get string wrapped in double quotes. For instance, when I type JSON.stringify({})
in Firefox and chrome console, I get back "{}"
instead of '{}'
.
根据MDN JSON.stringify上的示例,它应该返回一个用单引号括起来的字符串。但是,当我在该页面中尝试相同的示例时,我将字符串包装在双引号中。例如,当我在Firefox和chrome控制台中键入JSON.stringify({})时,我会返回“{}”而不是“{}”。
How can I properly serialize my Javascript object so the outer quotes are: '
. Again, this string is an example of what I want to achieve:
如何正确序列化我的Javascript对象,以便外部引号为:'。同样,这个字符串是我想要实现的一个例子:
'{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}'
Ideally, I would like to learn an nice way to serialize the object instead of having to modify the string after serialization.
理想情况下,我想学习一种很好的方法来序列化对象,而不必在序列化后修改字符串。
Edit: The reason I think I need to do this is that the API I am working with is somehow not happy when the string is wrapped in double quotes. For example when I do
编辑:我认为我需要这样做的原因是,当字符串用双引号括起来时,我正在使用的API不知何故。例如,当我这样做
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d "{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}" 'http://localhost:3000/api/blockies/17'
The request fails and server gives a parsing error. However when I try:
请求失败,服务器发出解析错误。但是,当我尝试:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}' 'http://localhost:3000/api/blockies/17'
The put request goes through successfully and the object is updated.
put请求成功完成,对象更新。
5 个解决方案
#1
3
There are no differences between strings wrapped in single or double quotes, besides escaping which is done automatically by the JSON.stringify
method. The single/double quotes which wrap string literals are not part of the string itself.
包含在单引号或双引号中的字符串之间没有区别,除了由JSON.stringify方法自动完成的转义。包装字符串文字的单引号/双引号不是字符串本身的一部分。
Double quotes is the way Firefox and Chrome prefer to represent string literals in the console.
双引号是Firefox和Chrome更喜欢在控制台中表示字符串文字的方式。
Edit: Now with the CURL command it changes the meaning of the question completely.
编辑:现在使用CURL命令可以完全改变问题的含义。
"{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}"
The string above is not a valid string as you can't have unescaped double quotes inside a double quote-wrapped string.
上面的字符串不是有效的字符串,因为在双引号包装的字符串中不能使用未转义的双引号。
#2
4
You don't need those single quotes wrapping the string - those are only there on the MDN page to show the string literals that correspond to the output.
您不需要包装字符串的那些单引号 - 这些仅在MDN页面上显示与输出对应的字符串文字。
The quotes are not part of the content of the strings themselves!
引号不是字符串本身内容的一部分!
EDIT - you've edited the question since I wrote the above.
编辑 - 自从我写完以上内容后你就编辑了这个问题。
The simple answer is that if you absolutely must wrap the string in single quotes yourself, just use:
简单的答案是,如果你必须自己用单引号包装字符串,只需使用:
var json = "'" + JSON.stringify(obj) + "'"
The longer answer is still that you shouldn't be wrapping the string at all. It's considered bad practise to pass entire command lines to a shell - the presence of certain environment variables (especially IFS
) can change the way that the command line is interpreted, leading to security issues.
更长的答案仍然是你根本不应该包装字符串。将整个命令行传递给shell被认为是不好的做法 - 某些环境变量(尤其是IFS)的存在可能会改变命令行的解释方式,从而导致安全问题。
Since you're using Javascript I guess perhaps you're using nodejs
and the child_process
module? If so, you should be using .spawn
instead of .exec
, and passing the parameters as an array. When passed this way the parameters are passed directly into Curl's argv
array without being parsed by the shell first, and therefore need no quoting at all, e.g.:
既然你正在使用Javascript我想你可能正在使用nodejs和child_process模块?如果是这样,您应该使用.spawn而不是.exec,并将参数作为数组传递。当以这种方式传递时,参数直接传递到Curl的argv数组中,而不是首先被shell解析,因此根本不需要引用,例如:
var child = spawn('curl', [
'-i', '-H', 'Accept: application/json',
'-H', 'Content-type: application/json',
'-X', 'PUT',
'-d', json,
'http://localhost:3000/api/blockies/17'
]);
or better yet make the PUT call directly from Node without using Curl.
或者更好的是直接从Node调用PUT,而不使用Curl。
#3
2
What you're seeing is just an artifact of how the console prints the string.
您所看到的只是控制台如何打印字符串的工件。
To wit, try this in Chrome's Console for fun...
也就是说,在Chrome的控制台中尝试这个以获得乐趣......
JSON.parse(
JSON.stringify(
JSON.parse(
JSON.stringify(
JSON.parse(
'{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}'
))))).name
#4
2
For instance, when I type JSON.stringify({}) in Firefox and chrome console, I get back "{}" instead of '{}'.
例如,当我在Firefox和chrome控制台中键入JSON.stringify({})时,我会返回“{}”而不是“{}”。
This is how strings are displayed in console. It's actually not "{}"
but a string that contains {}
. Quotes are just to signify that it's a string. Try typing '' in Chrome console and you'll see what I mean.
这是字符串在控制台中的显示方式。它实际上不是“{}”而是包含{}的字符串。行情只是表示它是一个字符串。尝试在Chrome控制台中输入'',您就会明白我的意思。
#5
1
The surrounding single/double quotes are just indicator that it is a string, it's not part of the actual String data and can't be stored.
周围的单引号/双引号只是指示它是一个字符串,它不是实际字符串数据的一部分,无法存储。
var serializedData = JSON.stringify({"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}});
var deserializedData = JSON.parse(serializedData); // whether console displays the serializedData String with single or double quotes is irrelevant
If you are storing the String and outputting it to the client side, wrap it with single quotes as you've done.
如果要存储String并将其输出到客户端,请使用单引号将其包装起来。
Is there anything that prevents you from wrapping it with single quotes when using curl?
在使用curl时,有什么东西阻止你用单引号包装吗?
If you insist on wrapping it with double quotes, you have to escape the double quotes in your string like so:
如果你坚持用双引号包装它,你必须在字符串中转义双引号,如下所示:
"{\"name\":\"Updated Blocki\",\"bounds\":{\"x\":\"2em\",\"y\":\"2em\",\"w\":\"8em\",\"h\":\"12em\"}}"
#1
3
There are no differences between strings wrapped in single or double quotes, besides escaping which is done automatically by the JSON.stringify
method. The single/double quotes which wrap string literals are not part of the string itself.
包含在单引号或双引号中的字符串之间没有区别,除了由JSON.stringify方法自动完成的转义。包装字符串文字的单引号/双引号不是字符串本身的一部分。
Double quotes is the way Firefox and Chrome prefer to represent string literals in the console.
双引号是Firefox和Chrome更喜欢在控制台中表示字符串文字的方式。
Edit: Now with the CURL command it changes the meaning of the question completely.
编辑:现在使用CURL命令可以完全改变问题的含义。
"{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}"
The string above is not a valid string as you can't have unescaped double quotes inside a double quote-wrapped string.
上面的字符串不是有效的字符串,因为在双引号包装的字符串中不能使用未转义的双引号。
#2
4
You don't need those single quotes wrapping the string - those are only there on the MDN page to show the string literals that correspond to the output.
您不需要包装字符串的那些单引号 - 这些仅在MDN页面上显示与输出对应的字符串文字。
The quotes are not part of the content of the strings themselves!
引号不是字符串本身内容的一部分!
EDIT - you've edited the question since I wrote the above.
编辑 - 自从我写完以上内容后你就编辑了这个问题。
The simple answer is that if you absolutely must wrap the string in single quotes yourself, just use:
简单的答案是,如果你必须自己用单引号包装字符串,只需使用:
var json = "'" + JSON.stringify(obj) + "'"
The longer answer is still that you shouldn't be wrapping the string at all. It's considered bad practise to pass entire command lines to a shell - the presence of certain environment variables (especially IFS
) can change the way that the command line is interpreted, leading to security issues.
更长的答案仍然是你根本不应该包装字符串。将整个命令行传递给shell被认为是不好的做法 - 某些环境变量(尤其是IFS)的存在可能会改变命令行的解释方式,从而导致安全问题。
Since you're using Javascript I guess perhaps you're using nodejs
and the child_process
module? If so, you should be using .spawn
instead of .exec
, and passing the parameters as an array. When passed this way the parameters are passed directly into Curl's argv
array without being parsed by the shell first, and therefore need no quoting at all, e.g.:
既然你正在使用Javascript我想你可能正在使用nodejs和child_process模块?如果是这样,您应该使用.spawn而不是.exec,并将参数作为数组传递。当以这种方式传递时,参数直接传递到Curl的argv数组中,而不是首先被shell解析,因此根本不需要引用,例如:
var child = spawn('curl', [
'-i', '-H', 'Accept: application/json',
'-H', 'Content-type: application/json',
'-X', 'PUT',
'-d', json,
'http://localhost:3000/api/blockies/17'
]);
or better yet make the PUT call directly from Node without using Curl.
或者更好的是直接从Node调用PUT,而不使用Curl。
#3
2
What you're seeing is just an artifact of how the console prints the string.
您所看到的只是控制台如何打印字符串的工件。
To wit, try this in Chrome's Console for fun...
也就是说,在Chrome的控制台中尝试这个以获得乐趣......
JSON.parse(
JSON.stringify(
JSON.parse(
JSON.stringify(
JSON.parse(
'{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}'
))))).name
#4
2
For instance, when I type JSON.stringify({}) in Firefox and chrome console, I get back "{}" instead of '{}'.
例如,当我在Firefox和chrome控制台中键入JSON.stringify({})时,我会返回“{}”而不是“{}”。
This is how strings are displayed in console. It's actually not "{}"
but a string that contains {}
. Quotes are just to signify that it's a string. Try typing '' in Chrome console and you'll see what I mean.
这是字符串在控制台中的显示方式。它实际上不是“{}”而是包含{}的字符串。行情只是表示它是一个字符串。尝试在Chrome控制台中输入'',您就会明白我的意思。
#5
1
The surrounding single/double quotes are just indicator that it is a string, it's not part of the actual String data and can't be stored.
周围的单引号/双引号只是指示它是一个字符串,它不是实际字符串数据的一部分,无法存储。
var serializedData = JSON.stringify({"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}});
var deserializedData = JSON.parse(serializedData); // whether console displays the serializedData String with single or double quotes is irrelevant
If you are storing the String and outputting it to the client side, wrap it with single quotes as you've done.
如果要存储String并将其输出到客户端,请使用单引号将其包装起来。
Is there anything that prevents you from wrapping it with single quotes when using curl?
在使用curl时,有什么东西阻止你用单引号包装吗?
If you insist on wrapping it with double quotes, you have to escape the double quotes in your string like so:
如果你坚持用双引号包装它,你必须在字符串中转义双引号,如下所示:
"{\"name\":\"Updated Blocki\",\"bounds\":{\"x\":\"2em\",\"y\":\"2em\",\"w\":\"8em\",\"h\":\"12em\"}}"