简体   繁体   中英

How to remove double quote from json

I want to remove double quote from only data node from json string.

current json:

[{"name":"Labor Shortage Index","data":["0.2","0.5","0.5","1.8"]}]

So after remove double quote json will be:

[{"name":"Labor Shortage Index","data":[0.2,0.5,0.5,1.8]}]

Please suggest.

Thanks

With applying Number for every element.

 var object = [{ "name": "Labor Shortage Index", "data": ["0.2", "0.5", "0.5", "1.8"] }]; object[0].data = object[0].data.map(Number); document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

  1. Parse the JSON
  2. Loop over the data array
  3. Overwrite each value with the result of calling parseFloat on it
  4. Stringify back to JSON

try this code

var obj = [{"name":"Labor Shortage Index","data":["0.2","0.5","0.5","1.8"]}];
obj = obj.map(function(value){
   value.data = value.data.map(function(innerValue){ return parseFloat(innerValue); });
   return value;
});

One answer would be to use an array map to parse the data node array:

 var parsedData = data.map(function(num){ return parseFloat(num); });

See array map and parseFloat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM