Transform serialized inputs into JSON

0

In a datatable there are some columns with inputs. If I click the Submit button I will serialize the inputs of the lines that are marked checkbox. The return of the serialize:

'id=1596&inputCodPag=dwad&inputDtPagto=22-03-2018&inputDtMalote=&inputTpPagamento=BB&id=2045&inputCodPag=dwad&inputDtPagto=&inputDtMalote=&inputTpPagamento=SAP'

What I want to know is, how to turn this into a JSON? The ideal would be to keep in the following structure:

[{id:1596,inputCodPag:dwad,inputDtPagto:'22-03-2018',inputDtMalote:'',inputTpPagamento:'BB'},{id:2045, inputCodPag:'dwad', inputDtPagto:''.inputDtMalote:'',inputTpPagamento:'SAP'}]

Thank you.

    
asked by anonymous 19.03.2018 / 17:09

1 answer

2

I do not think I have something native to this, but you can use this function ...

function deparam(query) {
    var pairs, i, keyValuePair, key, value, map = {};
    // remove leading question mark if its there
    if (query.slice(0, 1) === '?') {
        query = query.slice(1);
    }
    if (query !== '') {
        pairs = query.split('&');
        for (i = 0; i < pairs.length; i += 1) {
            keyValuePair = pairs[i].split('=');
            key = decodeURIComponent(keyValuePair[0]);
            value = (keyValuePair.length > 1) ? decodeURIComponent(keyValuePair[1]) : undefined;
            map[key] = value;
        }
    }
    return map;
}

let resul = deparam('id=1596&inputCodPag=dwad&inputDtPagto=22-03-2018&inputDtMalote=&inputTpPagamento=BB&id=2045&inputCodPag=dwad&inputDtPagto=&inputDtMalote=&inputTpPagamento=SAP');

console.log(JSON.stringify(resul));

Source: link

    
19.03.2018 / 17:19