0

I have serialized data on html page and want to process it.

My data format is like :

    "lusername=unm1&lpassword=p1&lpassword=p2""lusername=unm2&lpassword=unm2p1&lpassword=unmp2""wusername=unmw1&wpassword=pw1&wpassword=pw2
w2""wusername=unmw2&wpassword=pw1""snmpsettings=pqr""subnet=1.1.1.1"

I want to process above data to sort out final data string as :

lusername=unm1;lpassword=p1,p2;
lusername = unmw2; lpassword = pw1,pw2;
snmpsettings = pqr

How do deserilize my data to get above output?

earthmover
  • 4,395
  • 10
  • 43
  • 74
user3322141
  • 158
  • 6
  • 23

2 Answers2

0

You can do a split on a specific character see: How do I split a string, breaking at a particular character?

You probably will need to do multiple splits to give you the proper array structure and values.

Community
  • 1
  • 1
nhavar
  • 198
  • 1
  • 8
0

You're probably looking for something like this:

var data = '"lusername=unm1&lpassword=p1&lpassword=p2""lusername=unm2&lpassword=unm2p1&lpassword=unmp2""wusername=unmw1&wpassword=pw1&wpassword=pw2w2""wusername=unmw2&wpassword=pw1""snmpsettings=pqr""subnet=1.1.1.1"',
    objs = data.split(/"{1,2}/);
var newArr = [];
$.each(objs,function( i, item ) {
    if( item !== "" ) {
        var parts = item.split( '&' ),
            x = {};
        $.each( parts, function( index, value ) {
            var key = value.split( '=' )[0],
                val = value.split( '=' )[1];
            x[ key ] = ((!!x[ key ]) ? x[ key ] + ',' : '') + val;
        });
        newArr.push( x );
    }
});
console.log( newArr );

And here is a working demo: JS FIDDLE DEMO

PeterKA
  • 24,158
  • 5
  • 26
  • 48