使用JSON填充javascript变量

时间:2012-01-19 15:20:02

标签: javascript json cordova

我正在使用手机间隙,我想用手机间隙文件api读取文件。我可以读取该文件。但是当我将JSON放入变量时,它只返回'[]'。

var json;
function readFS(fileSystem) {
            fileSystem.root.getFile("readme.txt", {create: true}, readFileEntry, fail);
        }

        function readFileEntry(fileEntry) {
            fileEntry.file(gotFileread, fail);
        }

        function gotFileread(file){
            readAsText(file);
        }


        function readAsText(file) {
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read as text");
                console.log(evt.target.result);
                json = evt.target.result; // this returns only []
                navigator.notification.alert(evt.target.result); // this returns the real JSON :o

            };
            reader.readAsText(file);
        }

这是readme.txt中的json

{"games":[{"id":"2","name":"bartje","photo":"photo2.png","description":"kimpe","length":"is","totalDownloads":"0","totalFinished":"0","locations":[{"id":"3","name":"loc21","photo":"loc21.jpg","description":"loc21","FK_Game_id":"2","lat":"51.0000","long":"4.0000","status":"0"},{"id":"5","name":"loc22","photo":"loc22.jog","description":"locatie 22","FK_Game_id":"2","lat":"51.2330","long":"40.2222","status":"1"}]},{"id":"3","name":"aa","photo":"photo3.jPg","description":"aa","length":"aa","totalDownloads":"0","totalFinished":"3","locations":[{"id":"4","name":"loc4","photo":"loc4.jpg","description":"loc4","FK_Game_id":"3","lat":"51.2191","long":"4.4021","status":"0"}]}]}

我现在可以从文件中检索数据并将其放入变量中。现在我唯一的问题是我不能像json一样使用它

json.games[0].id

我试过

json = eval(evt.target.result);

但这不起作用:o

3 个答案:

答案 0 :(得分:2)

尝试:

myJson = JSON.parse(evt.target.result);

因为这将从您阅读的文本中创建一个对象。

答案 1 :(得分:0)

这样可行,因此可能与您阅读文件的方式有关。你在console.log(evt.target.result);看到了什么?

var g = {"games":[{"id":"2","name":"bartje","photo":"photo2.png","description":"kimpe","length":"is","totalDownloads":"0","totalFinished":"0","locations":[{"id":"3","name":"loc21","photo":"loc21.jpg","description":"loc21","FK_Game_id":"2","lat":"51.0000","long":"4.0000","status":"0"},{"id":"5","name":"loc22","photo":"loc22.jog","description":"locatie 22","FK_Game_id":"2","lat":"51.2330","long":"40.2222","status":"1"}]},{"id":"3","name":"aa","photo":"photo3.jPg","description":"aa","length":"aa","totalDownloads":"0","totalFinished":"3","locations":[{"id":"4","name":"loc4","photo":"loc4.jpg","description":"loc4","FK_Game_id":"3","lat":"51.2191","long":"4.4021","status":"0"}]}]}

alert(g.games[0].id) --> "2"

alert(g.games[0].locations[0].name) --> "loc21"

答案 2 :(得分:0)

如果evt.target.result是字符串

'{"games":[{"id":"2","name":"bartje","photo":"photo2.png","description":"kimpe","length":"is","totalDownloads":"0","totalFinished":"0","locations":[{"id":"3","name":"loc21","photo":"loc21.jpg","description":"loc21","FK_Game_id":"2","lat":"51.0000","long":"4.0000","status":"0"},{"id":"5","name":"loc22","photo":"loc22.jog","description":"locatie 22","FK_Game_id":"2","lat":"51.2330","long":"40.2222","status":"1"}]},{"id":"3","name":"aa","photo":"photo3.jPg","description":"aa","length":"aa","totalDownloads":"0","totalFinished":"3","locations":[{"id":"4","name":"loc4","photo":"loc4.jpg","description":"loc4","FK_Game_id":"3","lat":"51.2191","long":"4.4021","status":"0"}]}]}';

使用

将其转换为对象
var result = eval(evt.target.result);

然后你可以给

var games = dataObject.games;
for(var i = 0; i< games.length; i++){
     var game = games[i];
     // then access properties games.id, games.name, games.photo ... etc..
}