Search code examples
c#jsonunity-game-enginesimplejson

Save changes to JSON with SimpleJSON


I have a JSON file as follows:

{
  "volume" : "1.0"
}

I want to edit this to another value like 0.5 etc. When I use the following code I get strange results in my JSON file. It does write something to the JSON file but it writes strange symbols to the file with volume in between.

SimpleJSON.JSONNode node = SimpleJSON.JSONNode.Parse(Resources.Load<TextAsset>("JSON/Test/test").text);

// new value
node["volume"].AsFloat = 0.5f;

System.IO.BinaryWriter bw = new System.IO.BinaryWriter(File.Open("Assets/Resources/JSON/Test/test.json", 
FileMode.Create));

node.Serialize(bw);

What is wrong with my code?

I am using SimpleSON in an Unity project.


Solution

  • I have solved this but forgot to post the answer so here it is.

        SimpleJSON.JSONNode node = SimpleJSON.JSONNode.Parse(Resources.Load<TextAsset>
        ("JSON/Test/test").text);
    
        node["volume"].AsFloat = 0.5f;
    
        File.WriteAllText(Environment.CurrentDirectory + "/Assets/Resources/JSON/Test/" + @"\audio.json", node.ToString());