简体   繁体   English

用正斜杠逃避json字符串?

[英]escaping json string with a forward slash?

I am having a problem passing a json string back to a php script to process. 我有一个问题将json字符串传递回PHP脚本进行处理。

I have a json string that's been created by using dojo.toJson() that contains a / and looks like this: 我有一个json字符串,它是使用包含/的dojo.toJson()创建的,如下所示:

[{"id":"2","company":"My Company / Corporation","jobrole":"Consultant","jobtitle":"System Integration Engineer"}]

When I pass the string back to the php script it get's chopped at the / and creates a malformed json string, which then means I can't convert it into a php array. 当我将字符串传递回php脚本时,它会被删除/并创建一个格式错误的json字符串,这意味着我无法将其转换为php数组。

What is the best way of escaping the / in this string? 在这个字符串中转义/的最佳方法是什么? I was looking at regular expressions and doing a string.replace() however my regex isn't that strong, and I'm not sure if there are better ways of doing this? 我正在查看正则表达式并执行string.replace()但是我的正则表达式并不那么强大,而且我不确定是否有更好的方法可以做到这一点?

Many thanks 非常感谢

You shouldn't need to do anything special to represent a / in JSON - a string can contain any character except a " or (when not used to start an escape sequence) \\ . 你不应该需要做什么特别的表示/在JSON -字符串可以包含除任何字符一个"或(不使用时启动一个转义序列) \\

The problem is possibly therefore in: 问题可能在于:

  • the way you parse the JSON server side 解析JSON服务器端的方式
  • the way your parse the HTTP data to get the JSON string 解析HTTP数据以获取JSON字符串的方式
  • the way you encode the string before making the HTTP request 在发出HTTP请求之前对字符串进行编码的方式

(I'd bet on it being the last of those options). (我敢打赌它是最后一个选项)。

I would start by using a tool such as LiveHttpHeaders or Charles Proxy to see exactly what data is sent to the server. 我将首先使用LiveHttpHeaders或Charles Proxy等工具来确切地查看发送到服务器的数据。

(I'd also expand the question with the code you use to make the request, and the code you use to parse it at the other end). (我还会使用您用来发出请求的代码扩展问题,以及用于在另一端解析它的代码)。

\\/ . \\/ Take a look here . 看看这里 The documentation is really easy to read, concise and clear. 该文档非常易于阅读,简洁明了。 But unescaped / should still be valid in JSON's string so maybe your bug is somewhere else? 但是未转义/应该仍然在JSON的字符串中有效,所以也许你的bug在别的地方?

Ok. 好。 Anyway. 无论如何。

When passing variables to PHP don't use JSON - it's good for passing variables other way. 将变量传递给PHP时不要使用JSON - 这对于以其他方式传递变量很有用。

Instead you better use http://api.dojotoolkit.org/jsdoc/1.3/dojo.objectToQuery method and on PHP side parse standard PHP $_GET variables. 相反,你最好使用http://api.dojotoolkit.org/jsdoc/1.3/dojo.objectToQuery方法,并在PHP端解析标准的PHP $_GET变量。

EDIT: Ok, I'm 'lost in the woods' here also, but here's a tip - check if you don't have some mod_rewrite rules in action here. 编辑:好的,我也在这里'迷失在树林里',但这里有一个提示 - 检查你是否在这里没有一些mod_rewrite规则。 Kind of seems like that. 有点像那样。

Also, if you can send me the URL which gave you 404 (you can cut out domain part, i'm interested in script filename and all afterwards) maybe I can give you more detailed answer. 另外,如果你能给我发送给你404的URL(你可以删除域名部分,我对脚本文件名感兴趣,之后都是),也许我可以给你更详细的答案。

To be clear, whether you choose to send JSON to PHP or use regular form values is a matter of preference. 要明确的是,您是选择将JSON发送到PHP还是使用常规表单值是一个优先事项。 It /should/ work either way. 它/应该/工作两种方式。 It sounds like you aren't url-encoding the JSON at the client-side so the server-side is treating / as a path delimiter. 听起来你不是在客户端对URL进行url编码,因此服务器端将/视为路径分隔符。 In which case its borked before json_decode gets to it. 在这种情况下,它在json_decode之前被borked到达它。

so, try encodeURIComponent( dojo.toJson(stuff) ) 所以,尝试encodeURIComponent(dojo.toJson(stuff))

json_encode() used to escape forward slashes. json_encode()用于转义正斜杠。 like this: 像这样:

prompt> json_encode(json_decode('"A/B"'));
string(6) ""A\/B""

JSON_UNESCAPED_SLASHES was added in PHP5.4 to suppress this behavior. PHP5.4中添加了JSON_UNESCAPED_SLASHES来抑制此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM