Javascript JSON.Parse method fails on clean json data - syntax error unexpected token -
i have js file contains json nodes. file read program, string, , run json.parse on it:
var client = new xmlhttprequest(); client.open('get', 'data/data.js'); //when file has been loaded, execute client.onreadystatechange = function() { if(client.responsetext != "") { scantext(client.responsetext); } } function scantext(text) { var json; try { var cleanedtext = text; cleanedtext = cleanedtext.replace('var', ''); cleanedtext = cleanedtext.replace('arrayname', ''); cleanedtext = cleanedtext.replace('=','') alert(cleanedtext); json = json.parse(cleanedtext); //issue happens here alert('try'); } catch (ex) { alert(ex); } } my datafile looks like:
[ { aollname:'yui678', contract:'123-33' }, { tollname:'yui678', contract:'123-33' } ] i error 'syntax error: unexpected token a', comes first node aollname.
why cant json.parse method run on input?
your file not contain valid json.
json keys must quoted, , string values must use double quotes. because might execute valid javascript object, not mean valid json (the data interchange format).
[ { "aollname": "yui678", "contract": "123-33" }, { "tollname": "yui678", "contract": "123-33" } ] would valid.
Comments
Post a Comment