LitJson的使用教程

下载LitJson.dll

放进你的工程

序列化

public static void SaveData()
    {
        string FileName = "Assets/Data/Archice.json";
        StreamReader reader = File.OpenText(FileName);
        string input = reader.ReadToEnd();
        Dictionary<string, List<Dictionary<string, object>>> jsonObject = JsonMapper.ToObject<Dictionary<string, List<Dictionary<string, object>>>>(input);
        jsonObject["Archice"][0]["MapID"] = MapManager.CurrentMapIndex;//获得当前地图的索引
        reader.Close();//记得关闭
        reader.Dispose();
        //保存数据
        string values = JsonMapper.ToJson(jsonObject);//把对象序列化为Json
        Debug.Log(values);
        FileInfo file = new FileInfo(FileName);
        StreamWriter writer = file.CreateText();
        writer.WriteLine(values);//写数据到Json
        writer.Close();
        writer.Dispose();
    }

反序列化

 public void ReadJson()
    {
        string FilePath = Application.streamingAssetsPath + "/JsonPerson.json";        
        StreamReader json01 = File.OpenText(FilePath);       
        JsonData person = JsonMapper.ToObject(json01);
        //person就是你序列化出来的东西了
    }

总结:相对于Unity 自带的JsonUtility类而言,LitJson更胜一筹,它可以对一些略显复杂的类进行操作,而JsonUtility大多数情况下仅用来对一些简单的类进行操作

附录:
c# Json文件操作(JsonUtility)
Json序列化工具 Newtonsoft.Json的使用

posted @ 2022-06-22 18:05  哒哒哒~~~  阅读(104)  评论(0编辑  收藏  举报