I need convert json object to url form like: "parameter=12&asd=1"
I done with this:
var data = { 'action':'actualiza_resultado', 'postID': 1, 'gl': 2, 'gl2' : 3 }; var string_=JSON.stringify(data); string_=string_.replace(/{/g, ""); string_=string_.replace(/}/g, ""); string_=string_.replace(/:/g, "=") string_=string_.replace(/,/g, "&"); string_=string_.replace(/"/g, "");
But i wonder if there any function in javascript or in JSON object to do this?
JSON→URL text is limited to a subset of the characters allowed in a URL query string. All other characers must be percent encoded. The [ , ] , { , and } characters are not allowed in a URL query string so objects and arrays both begin with ( and end with ) .
Get JSON From URL Using jQuery Usually, jQuery. getJSON(url, data, success) is the signature method for getting JSON from an URL. In this case, the URL is a string that ensures the exact location of data, and data is just an object sent to the server.
The short answer is yes Javascript can parse URL parameter values. You can do this by leveraging URL Parameters to: Pass values from one page to another using the Javascript Get Method. Pass custom values to Google Analytics using the Google Tag Manager URL Variable which works the same as using a Javascript function.
jQuery provides param
that does exactly that. If you don't use jquery, take at look at the source.
Basically, it goes like this:
url = Object.keys(data).map(function(k) { return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }).join('&')
2019 update: there's now a built-in object URLSearchParams
for this type of thing:
let myParams = {'foo': 'hi there', 'bar': '???'}; let u = new URLSearchParams(myParams).toString(); console.log(u);
Using ES6 syntax:
var data = { 'action':'actualiza_resultado', 'postID': 1, 'gl': 2, 'gl2' : 3 }; let urlParameters = Object.entries(data).map(e => e.join('=')).join('&'); console.log(urlParameters);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With