json - Replace quotes by double quotes in javascript -
i search regex or other way replace quotes double quotes. keep quote in word. example keep quote in jusqu'à
. try find regular expression didn't find.
input:
{'nom': 'mulot', 'code': 'apodemus agrarius', 'descriptif': 'mammifère', 4'reconnaissance': "petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.", 'conditions': ' ', 'sources': ' '}
output:
{"nom": "mulot", "code": "apodemus agrarius", "descriptif": "mammifère", "reconnaissance": "petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.", "conditions": ' ', "sources": " "}
thanks lot.
well need split on commas, loop on that, split on colons, loop on that, replace quotes, join together, , join first split together.
var str = "{'nom': 'mulot', 'code': 'apodemus agrarius', 'descriptif': 'mammifère', 'reconnaissance': \"petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.\", 'conditions': ' ', 'sources': ' '}"; var rep = "{" + str.substr(1, str.length - 2).split(/\s?,\s?/).map(function(v) { return v.split(/\s?:\s?/).map(function(p) { return p.replace(/^\s?'([^']+)'\s?$/, '"$1"'); }).join(":"); }).join(",") + "}"; console.log(rep);
problems this"
- it not check
"
inside of string. cause issue. - if string contains
,
break - if string contains
:
break - does not handle arrays/objects inside
another solution (which not 100% safe) eval() or new function()
var str = "{'nom': 'mulot', 'code': 'apodemus agrarius', 'descriptif': 'mammifère','reconnaissance': \"petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.\", 'conditions': ' ', 'sources': ' '}"; var arr = new function("return " + str)(); console.log(arr); console.log(json.stringify(arr));
now best solution, fix whatever outputting/inputting in first place.
Comments
Post a Comment