Home > database >  I can't write json file with c#
I can't write json file with c#

Time:10-25

I just want to save an array in a json file. But I don't know what's going on. In dataUser.json there is only "{}"

//test.cs
void Start() { Test(); }
void Test()
{
    string[,] dataUser = new string[,] 
    {
        {"NgocPhat", "03"},
        {"PhatNgoc", "02"}
    };
    string json = JsonUtility.ToJson(dataUser, true);
    File.WriteAllText(Application.dataPath   "/dataUser.json", json);    
}

Output

//dataJson.json
{}

//https://i.stack.imgur.com/SRDHJ.png

CodePudding user response:

The documentation linked from someone's comment probably points to the problem:

"Internally, this method uses the Unity serializer; therefore the object you pass in must be supported by the serializer: it must be a MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute applied."

My guess is that the base array class doesn't have that attribute? I think that a property of type array could be serialized, but maybe not the way you're doing it.

I'm guessing the solution will be to make a dummy class to serialize like this:

public class Dummy
{
    [Serializable]
    public string[,] Data { get; set; }
}

string[,] dataUser = new string[,] 
{
    {"NgocPhat", "03"},
    {"PhatNgoc", "02"}
};
string json = JsonUtility.ToJson(new Dummy { Data = dataUser }, true);

FYI, that will nest the array inside the Data property in the JSON.

You could also try something like Newtonsoft.JSON which might be a little more forgiving with your original code.

CodePudding user response:

not sure if this exactly answers your question. But i have been using json de & / serializing for a while and it works fine with this method!

So first you want to save / write stuff to a json file. (A list in this case)

                JsonSerializer serializer = new JsonSerializer();
                serializer.Converters.Add(new JavaScriptDateTimeConverter());
                serializer.NullValueHandling = NullValueHandling.Ignore;

                using (StreamWriter sw = new StreamWriter(@"YOURPATH"))
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, loaned); //loaned is the name of the list.
                }

Put this in a function Save() and then just call it or whatever method // way you want to do it.

Then if you want to read the informatino in the json file, // load the data.

                JsonSerializer serializer = new JsonSerializer();
                serializer.Converters.Add(new JavaScriptDateTimeConverter());
                serializer.NullValueHandling = NullValueHandling.Ignore;


                using (StreamReader file = File.OpenText(@"YOURPATH"))
                {
                    loaned = JsonConvert.DeserializeObject<List<Bok>>(File.ReadAllText(@"YOURPATH"));
                    serializer = new JsonSerializer();
                    loaned = (List<Bok>)serializer.Deserialize(file, typeof(List<Bok>)); //loaned = the list, and then the type list, and the class/object "Bok"
                }

Disclaimer : Im not an expert and im sure there are more effect methods but this one works just fine!

  • Related