Convert comma-separated string to json

0

I have the function:

$data ={
$G,3,"027",8,"sao paulo"
$H,3,"027",5,"curitiba"
$SP,3,"027",5,"brasilia",62312

}

          var dados = data.split(",");
          var json = [];
          var toSplit = dados.split(",");
          for (var i = 0; i < toSplit.length; i++) {
              json.push({
                      value : toSplit[0],
                      dados:[{
                        info: toSplit[i],
                      }]
             });
           }

The problem is that it does not return correctly, the expected one would be:

{ "$G": { "0": 3, "1": "027", "2": 8, "3": "sao paulo"

}, "$H": { "0": 3, "1": "027", "2": 5, "3": "curitiba"

}, "$SP": { "0": 3, "1": "027", "2": 5, "3": "brasilia", "4": 62312 } }

I'd like to know where I'm wrong ..

tks

    
asked by anonymous 16.09.2018 / 22:12

0 answers