I read my json file using readAsText functions with http and cordova.
The http request returns an object that is in order.
The cordova file readAsText returns a string containing the optional characters "r \ n \". This makes it impossible to use 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));
}, failed );
How to read JSON files using cordova?
UPDATE:
The funny thing is that the following works:
a = '{\r\n"a":"1",\r\n"b":"2"\r\n}';
b = JSON.parse(a);
so the problem is not only with \ r \ n ... there is something else added by cordova readAsText
UPDATE2
as the workaround I'm using now var object = eval("(" + res + ")")
Keep looking for a general way to load json objects ...
source
share