0

I have a JSON file that is given as:

{
    "Person": "true",
    "Age": "true",
    "Location": "false",
    "Phone": "true"
}

I am able to read it in Unity by using the code below. I am using SimpleJSON library.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using SimpleJSON;
using UnityEngine;
using UnityEngine.UI;

public class ReadWriteScene : MonoBehaviour {
    public string jsonFile;
    JSONNode itemsData;
    string path;

    // Start is called before the first frame update
    void Start () {

        path = Path.Combine (Application.streamingAssetsPath, "Settings.json");
        if (File.Exists (path)) {
            jsonFile = File.ReadAllText (path);
            DeserializePages ();
        }
    }

    void Update () {
        
    }

    public void DeserializePages () {
        itemsData = JSON.Parse (jsonFile);
        var parseJSON = JSON.Parse (jsonFile);

        Debug.Log(parseJSON["Phone"].Value);

    }

}

But I do not know how to write or make changes to the JSON via code? For example, how do I change the attribute "Age" to "false"?

Vika
  • 419
  • 1
  • 13
  • Get a JSON library? – visc Nov 23 '22 at 14:04
  • You need to make a class. Convert the json to the class, change whet you eanted and put the class back to json and save it. - or cheat and look for the property and value in the string and change them – BugFinder Nov 23 '22 at 14:04
  • I am using a SimpleJSON library and can it be used to write then? If so how? – Vika Nov 23 '22 at 14:30
  • Does this answer your question? [Change values in JSON file (writing files)](https://stackoverflow.com/questions/21695185/change-values-in-json-file-writing-files) – Zakaria Najim Nov 23 '22 at 14:53
  • Simply search on how to serialize and deserialize a .json file which is the best approach when it comes to work with JSON files and data. – Pavlos Mavris Nov 23 '22 at 15:04

3 Answers3

1

It's very easy.

itemsData["Age"] = "false";

Check https://docs.unity3d.com/Packages/com.unity.systemgraph@2.0/api/SimpleJSON.JSONNode.html.

Mauro Vanetti
  • 424
  • 1
  • 5
  • 18
0
itemsData["Age"] = "false";
File.WriteAllTextAsync(path, itemsData.ToString());
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Nov 24 '22 at 14:31
0

It was quite easy. I used JSONObject and did the following:

public void SaveData(){
        JSONObject json = new JSONObject();
        json.Add("Person", "true");
        json.Add("Age", "false");
        json.Add("Location", "true");
        json.Add("Phone", "true");

        File.WriteAllText(path, json.ToString());
    }

Know that, I did not create a new file, it replaces the already created file but make sure you add all the given attributes or else it will get erased.

Vika
  • 419
  • 1
  • 13