How to save/restore serializable object to/from file?

前端 未结 6 880
旧时难觅i
旧时难觅i 2020-11-22 11:17

I have a list of objects and I need to save that somewhere in my computer. I have read some forums and I know that the object has to be Serializable. But it wou

6条回答
  •  误落风尘
    2020-11-22 11:44

    You can use JsonConvert from Newtonsoft library. To serialize an object and write to a file in json format:

    File.WriteAllText(filePath, JsonConvert.SerializeObject(obj));
    

    And to deserialize it back into object:

    var obj = JsonConvert.DeserializeObject(File.ReadAllText(filePath));
    

提交回复
热议问题