Search Unity

How do I Change an Information inside a Json File?

Discussion in 'Scripting' started by FGPArthurVII, Dec 5, 2016.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    Hello, I'm using json to store data from my application, let's suppose I have something like '"score" : 32' and at some point in the game I have to update it trough unity (saving player's score, ths kinda stuff) How do I do it/ what is the C# code for it, without having to rewrite the entire script for each information I change?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    using some json parser you turn the string into an object, modify the property, then turn it back into the json string.

    Unity has built in json parser methods:
    https://docs.unity3d.com/ScriptReference/JsonUtility.html

    And an article on using it:
    https://docs.unity3d.com/Manual/JSONSerialization.html

    Of course, their technique treats json as a serialization technique for unity objects. If you don't have classes defined for the objects being serialized, you won't be able to do it.

    There are 3rd party json parsers though that are very generic.

    For example SimpleJSON found on the unify wiki:
    http://wiki.unity3d.com/index.php/SimpleJSON

    (I have no experience with this tool, I am not giving it a good review... just demonstrating that 3rd party options exist)