Do you know a fast and simple way to encode a Javascript Object into a No |
|||||||||||||||||
|
like this?
Edit: this one also converts recursive objects (using php "array" notation for the query string)
|
|||||||||||||||||||||
|
jQuery has a function for this, example:
|
|||||||||||||||||||||
|
Edit: I like this one-liner, but I bet it would be a more popular answer if it matched the accepted answer semantically:
|
|||||||||||||||||||||
|
Here's a one liner in ES6:
|
|||||||||||||||||||||
|
With Node.js v6.6.3
Reference: https://nodejs.org/api/querystring.html |
|||||||||||||
|
A small amendment to the accepted solution by user187291:
Checking for hasOwnProperty on the object makes JSLint/JSHint happy, and it prevents accidentally serializing methods of the object or other stuff if the object is anything but a simple dictionary. See the paragraph on for statements in this page: http://javascript.crockford.com/code.html |
|||
Do you need to send arbitrary objects? If so, GET is a bad idea since there are limits to the lengths of URLs that user agents and web servers will accepts. My suggestion would be to build up an array of name-value pairs to send and then build up a query string:
|
|||
use JSON. take a look at this question for ideas on how to implement. |
|||||||||||||||||
|
Here's the coffeescript version of accepted answer. This might save time to someone.
|
|||||
|
Rails / PHP Style Query BuilderThis method converts a Javascript object into a
Example Usage:
|
|||||
|
I suggest using the URLSearchParams interface:
|
|||||
|
If you want to convert a nested object recursively and the object may or may not contain arrays (and the arrays may contain objects or arrays, etc), then the solution gets a little more complex. This is my attempt. I've also added some options to choose if you want to record for each object member at what depth in the main object it sits, and to choose if you want to add a label to the members that come from converted arrays. Ideally you should test if the thing parameter really receives an object or array.
|
|||||
|
Here's a concise & recursive version with Object.entries. It handles arbitrarily nested arrays, but not nested objects. It also removes empty elements:
E.g.:
|
||||
ok, it's a older post but i'm facing this problem and i have found my personal solution.. maybe can help someone else..
|
|||
Addition for accepted solution, this works with objects & array of objects:
Also have added objName if you're using object parameters like in asp.net mvc action methods. |
|||
A little bit look better
|
||||
This one skips null/undefined values
|
|||
The above answers fill not work if you have a lot of nested objects. Instead you can pick the function param from here - https://github.com/knowledgecode/jquery-param/blob/master/jquery-param.js It worked very well for me!
|
|||
I have a simpler solution that does not use any third-party library and is already apt to be used in any browser that has "Object.keys" (aka all modern browsers + edge + ie): In ES5
In ES3
|
|||
Just another way (no recursive object):
|
|||||
|
Refer from the answer @user187291, add "isArray" as parameter to make the json nested array to be converted.
To make the result : staffId=00000001&Detail[0].identityId=123456&Detail[1].identityId=654321
|
https://stackoverflow.com/questions/1714786/query-string-encoding-of-a-javascript-object