简体   繁体   中英

Javascript replacing tab char with html unicode for JSON parsing

In IE10, to parse a JSON string which contains tab char it needs to be in unicode

for example:

jsonString="{\"mynameproperty\":\"Chetan    Shettigar\"}"

Parsing of above string fails in IE10 because there is a tab char "Chetan Shettiga"

The acceptable char string Chetan	Shettigar

I am looking for a solution which can replace tab to respective html unicode. May be a regular expression can solve this problem, but is there any other easy way?

To replace all tabs with the html char code 	 , you can use the replace() function, like this:

jsonString="{\"mynameproperty\":\"Chetan    Shettigar\"}".replace(/\t/g, '	');

Although as mentioned in the comments, you should be using \\t instead of html char codes, like this:

jsonString="{\"mynameproperty\":\"Chetan    Shettigar\"}".replace(/\t/g, '\\t');

Try this:

var jsonString="{\"mynameproperty\":\"Chetan    Shettigar\"}";
jsonString = jsonString.replace(/\t/g, "\\t");
var obj = JSON.parse(jsonString);
document.body.innerHTML = obj.mynameproperty;

DEMO in JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM