Search Unity

Saving and Loading Data Using JSON

Discussion in 'Scripting' started by AbsoluteZero, Sep 11, 2014.

  1. AbsoluteZero

    AbsoluteZero

    Joined:
    Sep 2, 2013
    Posts:
    19
    Hello fellow Developers,

    I am having trouble trying to save and track my game data.

    I have a couple of entities which I would like to keep track off.
    For example:
    * Player Total Score
    * Player State
    * Which levels have been played
    * Which levels have been completed by the player
    * Number of times each level has been played

    I do not want to use playerprefs because my data will surely grow in size and complexity and managing playerprefs can be a nightmare.

    I have looked into storing my data in JSON using the SimpleJSON library.

    So I created a file with the following structure:

    {
    "Player_Name":"John",
    "Levels_Played":"4",
    "Levels_Finished":"2",
    "Player_Currency":"10",
    "Level1_Completed":"false"
    }

    I am able to parse the file and access my information but my problem is with trying to store and write new data to the file.

    After parsing the file with the following code:

    var N = JSON.Parse(the_JSON_string);

    I can do the following:

    var Levels_Played = N["Levels_Played"].Value; //this returns 4

    However, I am having problems if I need to update this value to 5.

    N["Levels_Played"] = 5; I can do this fine but it only updates it in memory. Is there any way I can write this update to the json file?

    I am open for other options too.

    Thank you
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Look at the library's many SaveTo* methods.