您当前的位置: 首页 >  json

unity工具人

暂无认证

  • 3浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

LitJson的使用教程

unity工具人 发布时间:2020-08-04 14:45:47 ,浏览量:3

下载LitJson.dll

放进你的工程

序列化

public static void SaveData()
    {
        string FileName = "Assets/Data/Archice.json";
        StreamReader reader = File.OpenText(FileName);
        string input = reader.ReadToEnd();
        Dictionary jsonObject = JsonMapper.ToObject(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的使用

关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0364s