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 @   哒哒哒~~~  阅读(116)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示