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

时间:2013-09-04 11:11:43

标签: json curl

以下是符合此说明的命令行示例:

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

我已经尝试了''(没有转义),网址编码(在另一端没有urldecoded!)和''(引用消失!),但没有成功。

4 个答案:

答案 0 :(得分:53)

如果你用'unicode encoded'替换它(它是\ u0027),那么它可以工作:

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

奇怪,但值得知道!

答案 1 :(得分:14)

在这种情况下,通常的解决方法是将数据放入文件并发布。

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

然后调用:

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

这样可以避免在json中转义任何引号。

答案 2 :(得分:9)

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

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

答案 3 :(得分:2)

你的意思是如何正确地通过命令行传递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\"} } }"