admin管理员组

文章数量:1391995

var text = [{"name":"en3","value":234},{"name":"en4","value":135},{"name":"en1","value":335},{"name":"en2","value":310},{"name":"en5","value":1548}]

how to convert to json without "?

like:[{"name":"en3","value":234},and so on?

i have try to use JSON.parse('${resultData}'.replace(/"/g, '\\"')),but it throws error Uncaught SyntaxError: Unexpected token \

var text = [{"name":"en3","value":234},{"name":"en4","value":135},{"name":"en1","value":335},{"name":"en2","value":310},{"name":"en5","value":1548}]

how to convert to json without "?

like:[{"name":"en3","value":234},and so on?

i have try to use JSON.parse('${resultData}'.replace(/"/g, '\\"')),but it throws error Uncaught SyntaxError: Unexpected token \

Share Improve this question asked Jul 8, 2015 at 10:24 王奕然王奕然 4,0696 gold badges47 silver badges66 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

Just replace your regEx with /"/g, '"'

i.e the code should be JSON.parse('${resultData}'.replace(/"/g, '"'))

var text = "[{"name":"en3","value":234},{"name":"en4","value":135},{"name":"en1","value":335},{"name":"en2","value":310},{"name":"en5","value":1548}]"

remove " from above string as

var text=text.replace(/"/g, '"');

then parse text array as

var json=JSON.parse(text);

then check the vlaues as....

  for(var i=0;i<json.length;i++)
        { 
            console.log(json[i].name);
            console.log(json[i].value);
        }

Remove the \\:

'${resultData}'.replace(/&quot;/g, '"')

Better yet: don't encode the " as &quot; in the first place.

本文标签: javascripthow to convert json without ampquotStack Overflow