search

Home  >  Q&A  >  body text

javascript - var x = "[{'a':'xx'},{'b':'xxx'}]"; How to parse into json object in js

var x = "[{'a':'xx'},{'b':'xxx'}]";How to parse it into a json object in js

世界只因有你世界只因有你2772 days ago1252

reply all(3)I'll reply

  • 欧阳克

    欧阳克2017-07-05 11:05:23

    Idea: This is a non-standard json, first standardize it, for example, replace ' with ".

    var x = "[{'a':'xx'},{'b':'xxx'}]";
    x = x.replace(/[']/g, '"');
    var obj = JSON.parse(x);
    console.log(x, obj);

    Effect

    zhaojunlike@zhaojunlike-winos MINGW64 ~/Desktop
    $ node demo.js
    [{"a":"xx"},{"b":"xxx"}] [ { a: 'xx' }, { b: 'xxx' } ]

    reply
    0
  • 阿神

    阿神2017-07-05 11:05:23

    x = JSON.stringify(x);
    JSON.parse(x);

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 11:05:23

    const obj = JSON.parse(x)

    reply
    0
  • Cancelreply