用正斜杠转义json字符串? [英] escaping json string with a forward slash?

查看:258
本文介绍了用正斜杠转义json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,将json字符串传回到php脚本进行处理。

I am having a problem passing a json string back to a php script to process.

我有一个使用 dojo.toJson()创建的json字符串,其中包含一个/,并且如下所示:

I have a json string that's been created by using dojo.toJson() that contains a / and looks like this:

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

当我将字符串传回php脚本时在/并创建一个格式不正确的json字符串,这意味着我无法将其转换成一个php数组。

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.

什么是最好的方式来转义/在这个字符串?我正在查看正则表达式并做一个string.replace(),但是我的正则表达式并不那么强大,我不知道有没有更好的方法来实现?

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?

非常感谢

推荐答案

您不需要做任何特别的代替 / 在JSON中 - 字符串可以包含除之外的任何字符,或(当不用于启动转义序列时) \

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服务器端的方式

  • 您解析HTTP数据以获取JSON字符串的方式

  • 您编码字符串的方式在进行HTTP请求之前

(我会打赌它是最后一个选项)。

(I'd bet on it being the last of those options).

我将首先使用LiveHttpHeaders或Charles Proxy等工具来查看发送到服务器的数据。

I would start by using a tool such as LiveHttpHeaders or Charles Proxy to see exactly what data is sent to the server.

(I' d也扩大了这个问题您用于提出请求的e代码,以及用于在另一端解析的代码)。

(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).

这篇关于用正斜杠转义json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆