Jump to content

Reading and Writing data from and to JSON file respectively


rizvanhaider
 Share

Recommended Posts

I am making kids ABC learning game in phaser. I used jquery to read json file and used data in my game successfully but when I run that game on android the data was not shown there... because I think JQuery is server dependent and there is no server for mobile.. so know I want to read and also want to write (I want to write data to change application images and colors as user chose...)  or update json data in separate file .. how can I do that.. my json data is like.

 

[{
    "letter": "A",
    "lfor": "Apple",
    "ref":"Assets/Limages/1.png"
}, {
    "letter": "B",
    "lfor": "Banana",
    "ref":"Assets/Limages/2.png"
    
},.......]
Link to comment
Share on other sites

The JSON file only needs to be downloadable like any other asset, and it should load and be parsed correctly. If all else fails, you can simply paste your JSON into your JavaScript as a variable (or load it as a separate script file like you do Phaser itself) as JSON is valid JavaScript:

var JSONData = [{    "letter": "A",    "lfor": "Apple",    "ref":"Assets/Limages/1.png"}, {    "letter": "B",    "lfor": "Banana",    "ref":"Assets/Limages/2.png"    },.......];

Will result in JSONData[0].letter == "A" and so on.

Link to comment
Share on other sites

You can't write to files without some server-side code just via normal HTML and JavaScript. I imagine on Android if packaging it as an app it provides some file system access so you can write files but that's beyond my expertise. If you have a browser environment available you do have things such as Local Storage however, which you can write to and have it persist between sessions.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...