Parsing malformed JSON in JavaScript

前端 未结 5 619
别跟我提以往
别跟我提以往 2020-12-07 04:38

Thanks for looking!

BACKGROUND

I am writing some front-end code that consumes a JSON service which is returning malformed JSON. Specifically, the keys are n

5条回答
  •  没有蜡笔的小新
    2020-12-07 05:20

    something like this may help to repair the json ..

    $str='{foo:"bar"}';
    echo preg_replace('/({)([a-zA-Z0-9]+)(:)/','$1"$2"${3}',$str);
    

    Output:

    {"foo":"bar"}
    

    EDIT:

    var str='{foo:"bar"}';
    str.replace(/({)([a-zA-Z0-9]+)(:)/,'$1"$2"$3')
    

提交回复
热议问题