admin管理员组文章数量:1134232
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 :
/?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.
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.
Share Improve this question asked Jul 24, 2011 at 13:53 Matthieu NapoliMatthieu Napoli 49.5k48 gold badges188 silver badges270 bronze badges 1- See also stackoverflow.com/questions/21802866/… – w00t Commented Jun 9, 2015 at 8:48
6 Answers
Reset to default 251encodeURIComponent(JSON.stringify(object_to_be_serialised))
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:
- 2651 characters using
jQuery.param
- 1691 characters using
JSON.stringify + encodeURIComponent
- 821 characters using
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.
- for array: https://stackoverflow.com/a/2790686/467286
- for object.keys: https://stackoverflow.com/a/3937321/467286
You could use the encodeURIComponent
to safely URL encode parts of a query string:
var array = JSON.stringify([ 'foo', 'bar' ]);
var url = 'http://example.com/?data=' + encodeURIComponent(array);
or if you are sending this as an AJAX request:
var array = JSON.stringify([ 'foo', 'bar' ]);
$.ajax({
url: 'http://example.com/',
type: 'GET',
data: { data: array },
success: function(result) {
// process the results
}
});
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:
["option", "Fred's dog", "Bill & Trudy", "param=3"]
That data, URL-encoded as the data
param:
"data=%5B%27option%27%2C+%22Fred%27s+dog%22%2C+%27Bill+%26+Trudy%27%2C+%27param%3D3%27%5D"
Same, base64-encoded:
"data=WyJvcHRpb24iLCAiRnJlZCdzIGRvZyIsICJCaWxsICYgVHJ1ZHkiLCAicGFyYW09MyJd"
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.
Using encodeURIComponent()
:
var url = 'index.php?data='+encodeURIComponent(JSON.stringify({"json":[{"j":"son"}]})),
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
var valuesToPass = new Array(encodeURIComponent(VALUE_1), encodeURIComponent(VALUE_2), encodeURIComponent(VALUE_3), encodeURIComponent(VALUE_4));
data = {elements:JSON.stringify(valuesToPass)}
PHP
json_decode(urldecode($_POST('elements')));
Hope this helps!
本文标签: javascriptHow to escape a JSON string to have it in a URLStack Overflow
版权声明:本文标题:javascript - How to escape a JSON string to have it in a URL? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736792179a1953138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论