0

Json string having multiple data table string. I would like to split that JSON string to array format like this. I don't know how to convert this. Please help me to do that.

array[0] = 2:Dubstep;3:BoysIIMen;4:Sylenth1
array[1] = 11:Dubstep;12:BoysIIMen;13:Sylenth1

{"table":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]","table1":"[{value: 11,label: 'Dubstep'},{value: 12,label: 'BoysIIMen'},{value: 13,label:'Sylenth1'}]"}
user3710059
  • 25
  • 2
  • 11

1 Answers1

0

I'm going to bet that there are more elegant solutions, but I've been able to parse your json and place it into a standard javascript array with two elements.

The down side is that it is not a "general solution" to your question. It doesn't read the names of the variables stored and does the process in two steps.

At least this is a start, and hopefully we'll get some more professional responses.

FIDDLE

JS

var mytables = {"table": [
                          {value: 2, label: 'Dubstep' },
                          {value: 3, label: 'BoysIIMen' },
                          {value: 4, label: 'Sylenth1'}
                          ],
                "table1":[
                          {value: 11, label: 'Dubstep'},
                          {value: 12, label: 'BoysIIMen'},
                          {value: 13, label: 'Sylenth1'}
                          ]
               };

var myarray1 = [];
var myarray2 = [];
var finalarray = [];

for(var n=0; n < mytables.table.length; n++)
   {
       myarray1.push( mytables.table[n].value + ':' + mytables.table[n].label + ';' );
       myarray2.push( mytables.table1[n].value + ':' + mytables.table1[n].label + ';' );
    }

finalarray.push( myarray1 );
finalarray.push( myarray2 );

$('.putmehere1').html( finalarray[0] );
$('.putmehere2').html( finalarray[1] );
TimSPQR
  • 2,964
  • 3
  • 20
  • 29