如何转义 JSON 字符串以将其包含在 URL 中?
我想使用 Javascript 生成一个页面链接。页面的参数位于我以 JSON 序列化的 Javascript 数组中。
所以我想生成这样的 URL:
http://example.com/?data="MY_JSON_ARRAY_HERE"
我需要如何转义 JSON 字符串(数组序列化)以将其作为参数包含在 URL 中?
如果有使用 JQuery 的解决方案,我会爱它。
注意:是的,页面的参数需要位于数组中,因为参数很多。我想之后我会使用 bit.ly 来缩短链接。
Using Javascript, I want to generate a link to a page. The parameters to the page are in a Javascript array that I serialize in JSON.
So I would like to generate a URL like that :
http://example.com/?data="MY_JSON_ARRAY_HERE"
How do I need to escape my JSON string (array serialized) to include it as a parameter in a URL ?
If there's a solution using JQuery I'd love it.
Note: Yes, the parameters to the page need to be in an array because there are a lot of them. I think I'll use bit.ly to shorten the links afterwards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想做同样的事情。对我来说问题是我的网址太长了。我今天使用 Bruno Jouhier 的 jsUrl.js 库找到了一个解决方案。
我还没有非常彻底地测试过它。但是,这里是一个示例,显示使用 3 种不同方法对同一大对象进行编码后字符串输出的字符长度:
jQuery.param
JSON.stringify +encodeURIComponent
1691 个字符code>JSURL.stringify
显然 JSURL 具有对 js 对象进行 urlEncoding 的最优化格式。
https://groups.google.com/forum/? 的帖子? fromgroups=#!topic/nodejs/ivdZuGCF86Q
显示编码和解析的基准。
注意:经过测试,jsurl.js 库似乎使用了 ECMAScript 5 函数,例如 Object.keys、Array.map 和 Array.filter。因此,它仅适用于现代浏览器(不支持 ie 8 及以下浏览器)。然而,这些功能的填充可以使其与更多浏览器兼容。
I was looking to do the same thing. problem for me was my url was getting way too long. I found a solution today using Bruno Jouhier's jsUrl.js library.
I haven't tested it very thoroughly yet. However, here is an example showing character lengths of the string output after encoding the same large object using 3 different methods:
jQuery.param
JSON.stringify + encodeURIComponent
JSURL.stringify
clearly JSURL has the most optimized format for urlEncoding a js object.
the thread at https://groups.google.com/forum/?fromgroups=#!topic/nodejs/ivdZuGCF86Q
shows benchmarks for encoding and parsing.
Note: After testing, it looks like jsurl.js library uses ECMAScript 5 functions such as Object.keys, Array.map, and Array.filter. Therefore, it will only work on modern browsers (no ie 8 and under). However, are polyfills for these functions that would make it compatible with more browsers.
您可以使用
encodeURIComponent
安全地 URL对查询字符串的部分进行编码:或者如果您将其作为 AJAX 请求发送:
You could use the
encodeURIComponent
to safely URL encode parts of a query string:or if you are sending this as an AJAX request:
我会提供一个替代方案。有时,使用完全不同的编码会更容易,特别是当您处理的各种系统并不都以相同的方式处理 URL 编码的详细信息时。
您可以对数据进行 Base64 编码,而不是对数据进行 URL 编码。这样做的好处是编码数据非常通用,仅包含字母字符,有时尾随
=
。示例:JSON 字符串数组:
该数据,URL 编码为
data
参数:相同,base64 编码:
base64 方法可能会更短一些,但更重要的是它更简单。我在 cURL、Web 浏览器和其他客户端之间移动 URL 编码数据时经常遇到问题,通常是由于引号、嵌入
%
符号、通配符的 shell 扩展等原因造成的。 Base64 非常中性并且非常便携,因为它的输出中没有特殊字符。许多系统都使用这种方法。例如,在 Kubernetes 生态系统中,
kubectl edit Secret
表示 Base64 中的所有机密,以避免编码问题并使不可打印的二进制数据轻松复制和粘贴。I'll offer an alternative. Sometimes it's easier to use a completely different encoding, especially if you're dealing with a variety of systems that don't all handle the details of URL encoding the same way.
Rather than URL-encoding the data, you can base64-encode it. The benefit of this is the encoded data is very generic, consisting only of alpha characters and sometimes trailing
=
's. Example:JSON array-of-strings:
That data, URL-encoded as the
data
param:Same, base64-encoded:
The base64 approach can be a bit shorter, but more importantly it's simpler. I often have problems moving URL-encoded data between cURL, web browsers and other clients, usually due to quotes, embedded
%
signs, shell expansion of wildcards, and so on. Base64 is very neutral and very portable because there are no special characters in its output.Many systems use this approach. For example in the Kubernetes ecosystem,
kubectl edit secret
represents all secrets in base64 to avoid encoding issues and to make non-printable binary data easily copy-and-pastable.使用encodeURIComponent():
Using
encodeURIComponent()
:德兰给出的答案是完美的。只需添加它 - 如果有人想要命名参数或单独传递多个 JSON 字符串 - 下面的代码可能会有所帮助!
JQuery
PHP
希望这有帮助!
Answer given by Delan is perfect. Just adding to it - incase someone wants to name the parameters or pass multiple JSON strings separately - the below code could help!
JQuery
PHP
Hope this helps!