javascript - convert invalid JSON string to JSON -


i have invalid json string following,

"{one: 'one', two: 'two'}" 

i tried use json.parse convert object. however, not valid json string. there functions can convert invalid format valid json string or directly convert object?

if example syntax same real json, jsonlint says need double quote name , value.

in case only, use these replace calls:

var jsontemp = yourjson.replace((/([\w]+)(:)/g), "\"$1\"$2"); var correctjson = jsontemp.replace((/'/g), "\""); //yourjson = "{one: 'one', two: 'two'}" //jsontemp = "{"one": 'one', "two": 'two'}" //correctjson = "{"one": "one", "two": "two"}" 

however should try work valid json in first place.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -