2012-01-19 69 views
2

我正在使用電話差距,我想用手機差距文件api讀取文件。我可以讀取文件。但是當我把我的JSON放入一個變量時,它只返回'[]'。使用JSON填充JavaScript變量

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文件

{"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像

json.games[0].id 

我試圖做

json = eval(evt.target.result); 

但是,這並不工作:○

+0

這不會解決你的問題......但是,一旦你得到正確的JSON文本,你將需要eval()的入JSON變量。 –

回答

2

嘗試:

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

因爲這將創建一個從你讀過的文本對象

+0

這不起作用,它也返回[] –

+0

我剛纔意識到它可能是與'json'的名稱衝突。嘗試調用其他變量,如'myJson'。 –

+0

你是對的!它是'myJSON = jQuery.parseJSON(evt.target.result)'。現在我可以用它作爲真正的JSON –

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" 
+0

我得到JSON文件前面有很多空格: –

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.. 
} 
+0

var games = result.games你的意思是?但evt.target.result不會給出字符串 –

+0

儘可能避免使用eval()並使用更安全的JSON.parse()。 –