1

I need to convert and JS object to query string and this object can wrap nested objects, like this:

 var item = {
        field1: 100,
        field2: 200,
        field3: [ 1,2,3 ],
        field4: {
            field5: 300,
            field6: 'Quad',
            field7: [ 4,5,6]
        }
    };

To convert this object I'm using this function

var serialize2 = function(obj, prefix) {
        var str = [], p;
        for (p in obj) {
            if (obj.hasOwnProperty(p)) {
                var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
                str.push((v !== null && typeof v === "object") ?
                    serialize2(v, k) :
                encodeURIComponent(k) + "=" + encodeURIComponent(v));
            }
        }
        return str.join("&");
    };

And once converted my query string looks like this

field4[field5]=300&field4[field6]=Quad

instead of

field4.field5=300&field4.field6=Quad

And then my Java Spring Application can't bind this children object

How can I solve this issue?

2 Answers 2

1

Just change your convert function:

var serialize2 = function(obj, prefix) {
    var str = [], p;
    for (p in obj) {
        if (obj.hasOwnProperty(p)) {
            var k;
            if (Array.isArray(obj)) {
                k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
            } else {
                k = prefix ? prefix + "." + p : p, v = obj[p];
            }
            str.push((v !== null && typeof v === "object") ?
                serialize2(v, k) :
            encodeURIComponent(k) + "=" + encodeURIComponent(v));
        }
    }
    return str.join("&");
};
Sign up to request clarification or add additional context in comments.

2 Comments

It fixes my issue with nested objects but broke my array: "field1=100 &field2=200 &field3.0=1 &field3.1=2 &field3.2=3 &field4.field5=300 &field4.field6=Quad &field4.field7.0=4 &field4.field7.1=5 &field4.field7.2=6"
Ah, I didn't notice you used arrays as well. Changed my answer so it should work with how you're doing things.
1
  function generateQueryParam(parameterMap) {
        var queryString = '';
        var _keys = Object.keys(parameterMap);

        _keys.forEach(mapResolver);

        function mapResolver(k, i) {
            var value = parameterMap[k];
            if (i < _keys.length-1) {
                queryString += k + '=' + value + '&';
            } else {
                queryString += k + '=' + value;
            }
        }
        return queryString;
    }

enter image description here

3 Comments

FYI : this only do convert plain js object to query string.If you need to resolve for nested objects go with some recursive strategy
Could you explain what your code is supposed to do?
this screenshot will explain .It will convert js object to a Query string @Anzeo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.