Friday 15 February 2013

cordova readAsText returns json string that can't be parsed to JSON object -



cordova readAsText returns json string that can't be parsed to JSON object -

i read json file using http , cordova file readastext functions.

http request returns object ok.

cordova file readastext function homecoming 'string' contain "r\n\" symbols. create impossible utilize json.parse(evt.target.result)

function readjson(abspath, success, failed){ window.resolvelocalfilesystemurl(abspath, function (entry) { entry.file(function (file) { var reader = new filereader(); reader.onloadend = function (evt) { success(evt.target.result); }; reader.readastext(file); }, failed); }, failed); } readjson(cordova.file.datadirectory + 'my.json', function(res){ console.log(json.parse(res)); //here i've got parsing error due presence of r\n\ symbols }, failed );

how read json files using cordova?

update:

funny thing next works:

a = '{\r\n"a":"1",\r\n"b":"2"\r\n}'; b = json.parse(a);

so problem not \r\n... there else added cordova readastext

update2

as workaround utilize var object = eval("(" + res + ")")

still search mutual way load json objects...

json cordova

No comments:

Post a Comment