Unity 基础之LitJSON

最近接手一个项目,在项目中有用到Json的地方,根据网上找到的资料 客户端配置文件优化策略,综合评定发现LitJson还不错,于是找到Json下载的网站的网站下载对应的Json,进入LitJson后尽然提示我 404! WTF?!!!我可是会科学上网的人,于是去GitHub下载LitJSON,发现最近几天刚更新过,估计性能会更好,但是只有对应的源码,没有DLL,也不要紧,我已经帮你把源码转成DLL了,LitJSON对应DLL下载

更新目录

  • 更新 2018.05.18 目前测试发现如果转换类中的字典Key为int时会有报错的情况

下面来简单介绍下LitJson的使用方法,更多示例参考GitHub上的文档

示例所示,JsonMapper.ToJson将制定数据结构转换为Json字符,JsonMapper.ToObject<T>用于将Json字符转换为指定数据结构

img_6227ae42e9d4f1d365e05b3d71b77126.png
img_1d4535960948f69097a7e134c1cdd7dc.png

这个示例介绍的是活的JsonData也可以按照索引器的方式访问指定的元素

img_065ad2795715f189dbf9c9530e5695b2.png
image.png
img_d1729ab0dab5b1c917e3f65d410d54b0.png

最后一个示例就是 JsonReaderJsonWriter , 这两种类型实际上是该库的基础,并且JsonMapper类型建立在它们之上,所以开发人员可以将JsonReaderJsonWriter类视为LitJSON的低级编程接口。

img_084c2ae3e035bb606344e0b79a38dc6e.png
img_85f59a70f2db1fc3a5f03609091743f2.png

完整Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;
using LitJson;
using Custom.Log;
using System.Text;

public class LitJsonExample : MonoBehaviour
{
    void Start()
    {
        //PersonToJson();
        //JsonToPerson();

        //LoadAlbumData(json);

        PrintJson(sample);
        this.Log(new String('-', 100));
        WriterJson();
    }

    #region 方案一
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Birthday { get; set; }
    }
    public void PersonToJson()
    {
        Person HaiLan = new Person();

        HaiLan.Name = "菜鸟海澜";
        HaiLan.Age = 51;
        HaiLan.Birthday = new DateTime(2018, 05, 17);

        string JsonStr = JsonMapper.ToJson(HaiLan);

        this.Log(JsonStr);
    }

    public void JsonToPerson()
    {
        string json = @"
            {
                ""Name""     : ""菜鸟海澜"",
                ""Age""      : 2018,
                ""Birthday"" : ""05/17/2018 00:00:00""
            }";

        Person HaiLan = JsonMapper.ToObject<Person>(json);

        this.Log("JsonToPerson Is Name:" + HaiLan.Name);
    }

    #endregion

    #region 方案二
    string json = @"
          {
            ""album"" : {
              ""name""   : ""The Dark Side of the Moon"",
              ""artist"" : ""Pink Floyd"",
              ""year""   : 1973,
              ""tracks"" : [
                ""Speak To Me"",
                ""Breathe"",
                ""On The Run""
              ]
            }
          }
        ";

    public void LoadAlbumData(string json_text)
    {
        Debug.Log("Reading data from the following JSON string: " + json_text);

        JsonData data = JsonMapper.ToObject(json_text);

        // 像哈希表一样访问字典
        this.Log("Album's name: {0}", data["album"]["name"]);

        // 存储在JsonData实例中的标量元素可以转换为它们的自然类型
        string artist = (string)data["album"]["artist"];
        int year = (int)data["album"]["year"];

        this.Log("Recorded by {0} in {1}", artist, year);

        // 数组也像常规列表一样被访问
        this.Log("First track: {0}", data["album"]["tracks"][0]);
    }
    #endregion

    #region 方案三
    string sample = @"{
            ""name""  : ""菜鸟海澜"",
            ""age""   : 2018,
            ""awake"" : true,
            ""n""     : 2018.0517,
            ""note""  : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ]
          }";

    public  void PrintJson(string json)
    {
        JsonReader reader = new JsonReader(json);

        this.Log("{0,14} {1,10} {2,16}", "Token", "Value", "Type");
        this.Log(new String('-', 100));

        // Read()方法返回false时,没有其他内容可读
        while (reader.Read())
        {
            string type = reader.Value != null ?
                reader.Value.GetType().ToString() : "";

            this.Log("{0,14} {1,10} {2,16}",
                              reader.Token, reader.Value, type);
        }
    }

    public  void WriterJson()
    {
        StringBuilder sb = new StringBuilder();
        JsonWriter writer = new JsonWriter(sb);

        writer.WriteArrayStart();
        writer.Write(1);
        writer.Write(2);
        writer.Write(3);

        writer.WriteObjectStart();
        writer.WritePropertyName("color");
        writer.Write("blue");
        writer.WriteObjectEnd();

        writer.WriteArrayEnd();

        this.Log(sb.ToString());
    }
    #endregion
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值