Writing data to a json file

Hi,
I am having trouble with something that should be simple as all the tutorials don’t mention it.

I want to create a user creation page where all the different users and their stats are saved in a Json file.

I can add a single row of data, so 1 user. That user gets over written every time I make a new one.
So I could use it to save a score across scenes or variable states etc, just not what I wantit for.
I want to add multiple rows (if you compared it to a dbase).

I tried ggdata (GlitchGames) and loadsave by RobMiracle (https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK).
With load save I actually managed to add items that did not overwrite each other, but could not start from a fresh empty file.
I had to pre populate it, and it missed the first index number.

Might be cause I am getting tired. but this is the row of data I want to read for each user:

Users= {}  
Users[i]={id=i,firstname=firstName.text,lastname=lastName.text,group=group.text}  

There won’t be many users so setting up and SQLlite dbase seems like overkill, but it actually looks easier to me at the moment.
It just bugs me that I am using the two options incorrectly.

Thanks for reading [import]uid: 100901 topic_id: 37108 reply_id: 67108[/import]

Hi Juf,

you’re probably just missing something simple inside of your code. can you post the parts around the user create/add phase ? maybe where you load as well.

cheers,
dmc

ps, i don’t know if you saw my library to save JSON data, dmc_autostore. its best feature is that it saves all of your data automatically. also, there is a hook which you can check for at the start of your app to see if you have a new file or not. i’d be glad to help you get it working in your app.

https://developer.coronalabs.com/code/dmc-lib-auto-store

pps, if you decide to stay with one of the other libraries and want to use it across different scenes/modules, etc, just be sure that you reload your data file every time you create/enter any scene which makes modifications, otherwise you’ll run the risk of overwriting data.

[import]uid: 74908 topic_id: 37108 reply_id: 145367[/import]

Looking at your library now. I at least like your enthusiasm in the description :slight_smile: [import]uid: 100901 topic_id: 37108 reply_id: 145385[/import]

My table save routine just json.encodes your entire table. I don’t do any array counting, so if you’re missing data, its likely one of two reasons. 1. it’s not there. 2. since you are using an array style table, if say you have 5 records and #3 is nil, the json.encode function may stop when it hit’s the nil.

Normally if you try to load a file that isn’t there, it’s going to return nil and do nothing more. Its up to you to test for that. Saving doesn’t care if there is a previous file or not, it’s a simple write over what is there or create a new file if there isn’t any.

Users = loadsave.loadTable("mytable.json") -- or whatever you call it.  
if Users == nil then  
 Users = {}  
end  
-- populate your table  
loadsave.saveTable(Users,"mytable.json")  

[import]uid: 199310 topic_id: 37108 reply_id: 145413[/import]

Hi Juf,

you’re probably just missing something simple inside of your code. can you post the parts around the user create/add phase ? maybe where you load as well.

cheers,
dmc

ps, i don’t know if you saw my library to save JSON data, dmc_autostore. its best feature is that it saves all of your data automatically. also, there is a hook which you can check for at the start of your app to see if you have a new file or not. i’d be glad to help you get it working in your app.

https://developer.coronalabs.com/code/dmc-lib-auto-store

pps, if you decide to stay with one of the other libraries and want to use it across different scenes/modules, etc, just be sure that you reload your data file every time you create/enter any scene which makes modifications, otherwise you’ll run the risk of overwriting data.

[import]uid: 74908 topic_id: 37108 reply_id: 145367[/import]

Looking at your library now. I at least like your enthusiasm in the description :slight_smile: [import]uid: 100901 topic_id: 37108 reply_id: 145385[/import]

My table save routine just json.encodes your entire table. I don’t do any array counting, so if you’re missing data, its likely one of two reasons. 1. it’s not there. 2. since you are using an array style table, if say you have 5 records and #3 is nil, the json.encode function may stop when it hit’s the nil.

Normally if you try to load a file that isn’t there, it’s going to return nil and do nothing more. Its up to you to test for that. Saving doesn’t care if there is a previous file or not, it’s a simple write over what is there or create a new file if there isn’t any.

Users = loadsave.loadTable("mytable.json") -- or whatever you call it.  
if Users == nil then  
 Users = {}  
end  
-- populate your table  
loadsave.saveTable(Users,"mytable.json")  

[import]uid: 199310 topic_id: 37108 reply_id: 145413[/import]