Bart De Kimpe
Bart De Kimpe

Reputation: 359

Populating javascript variable with JSON

I'm using phone gap, and I want to read a file out with the phone gap file api. I can read the file. But when I put my JSON into a variable it only returns '[]'.

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);
        }

This is the json that's in 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"}]}]}

I now can retrieve the data from the file and put it in a variable. Now my only problem is I can't use it as json like

json.games[0].id

I tried doing

json = eval(evt.target.result);

But that doesn't work :o

Upvotes: 2

Views: 3587

Answers (3)

Simon MacDonald
Simon MacDonald

Reputation: 23273

Try:

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

as that will create an object from the text you've read in.

Upvotes: 2

Diode
Diode

Reputation: 25165

if evt.target.result the string

'{"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"}]}]}';

convert it to object using

var result = eval(evt.target.result);

then you can give

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

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

This works, so it may have something to do with how you're reading the file. What do you see in 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"

Upvotes: 0

Related Questions