如何将单引号转义为双引号

时间:2022-09-17 18:54:49

Here is an example of command line that fit this description :

下面的命令行示例符合以下描述:

curl  http://dumbdomain.com/solr/collection2/update/json -H
'Content-type:application/json' -d ' { "add": { "doc": { "uid":
"79729", "text" : "I''ve got your number"} } }'

I already tried \' (not escaped), url encoded (not urldecoded at this other end!) and '' (quote disappear!), without success.

我已经尝试了\'(没有转义)、url编码(在另一端没有url解码!)和“(引用消失!),但没有成功。

4 个解决方案

#1


49  

If you replace ' by unicode encoded ' (which is \u0027), then it works:

如果你用“unicode encoded u0027”替换“by unicode encoded encoded”(编码),那么它就可以工作:

curl http://dumbdomain.com/solr/collection2/update/json -H 'Content-type:application/json' -d ' { "add": { "doc": { "uid": "79729", "text" : "I\u0027ve got your number"} } }'

Strange, but worth to know!

奇怪,但值得知道!

#2


13  

An usual workaround in such cases is to put the data in a file and post.

在这种情况下,通常的解决方法是将数据放入文件和post中。

$ cat post.json
{ "add": { "doc": { "uid": "79729", "text" : "I've got your number"} } }

And then invoke:

然后调用:

curl -H "Content-type:application/json" --data @post.json http://dumbdomain.com/solr/collection2/update/json

This would obviate the need of escaping any quotes in the json.

这将避免转义json中的任何引号。

#3


7  

In case you're using Windows (this problem typically doesn't occur on *nix), you can pipe the output from echo to curl to avoid the escaping altogether:

如果您正在使用Windows(这个问题通常不会出现在*nix上),您可以将输出从echo管道传输到curl,以避免转义:

echo {"foo": "bar", "xyzzy": "fubar"} | curl -X POST -H "Content-Type: application/json" -d @- localhost:4444/api/foo

#4


2  

Do you mean how to get the JSON passed via the command line correctly? If you're using Windows then you need to be careful how you escape your string. It works if you use double quotes around the whole data string and then escape the double quotes for the JSON. For example:

您的意思是如何通过命令行正确地传递JSON ?如果你正在使用Windows,那么你需要小心如何转义你的字符串。如果您对整个数据字符串使用双引号,然后转义JSON的双引号,那么它就可以工作。例如:

curl http://dumbdomain.com/solr/collection2/update/json -H 'Content-type:application/json' -d "{ \"add\": { \"doc\": { \"uid\": \"79729\", \"text\" : \"I've got your number\"} } }"

#1


49  

If you replace ' by unicode encoded ' (which is \u0027), then it works:

如果你用“unicode encoded u0027”替换“by unicode encoded encoded”(编码),那么它就可以工作:

curl http://dumbdomain.com/solr/collection2/update/json -H 'Content-type:application/json' -d ' { "add": { "doc": { "uid": "79729", "text" : "I\u0027ve got your number"} } }'

Strange, but worth to know!

奇怪,但值得知道!

#2


13  

An usual workaround in such cases is to put the data in a file and post.

在这种情况下,通常的解决方法是将数据放入文件和post中。

$ cat post.json
{ "add": { "doc": { "uid": "79729", "text" : "I've got your number"} } }

And then invoke:

然后调用:

curl -H "Content-type:application/json" --data @post.json http://dumbdomain.com/solr/collection2/update/json

This would obviate the need of escaping any quotes in the json.

这将避免转义json中的任何引号。

#3


7  

In case you're using Windows (this problem typically doesn't occur on *nix), you can pipe the output from echo to curl to avoid the escaping altogether:

如果您正在使用Windows(这个问题通常不会出现在*nix上),您可以将输出从echo管道传输到curl,以避免转义:

echo {"foo": "bar", "xyzzy": "fubar"} | curl -X POST -H "Content-Type: application/json" -d @- localhost:4444/api/foo

#4


2  

Do you mean how to get the JSON passed via the command line correctly? If you're using Windows then you need to be careful how you escape your string. It works if you use double quotes around the whole data string and then escape the double quotes for the JSON. For example:

您的意思是如何通过命令行正确地传递JSON ?如果你正在使用Windows,那么你需要小心如何转义你的字符串。如果您对整个数据字符串使用双引号,然后转义JSON的双引号,那么它就可以工作。例如:

curl http://dumbdomain.com/solr/collection2/update/json -H 'Content-type:application/json' -d "{ \"add\": { \"doc\": { \"uid\": \"79729\", \"text\" : \"I've got your number\"} } }"