当前位置:   article > 正文

LitJson解析

litjson
{
  "Web":  [
      {
        "id": 1,
        "name": "名字"
      },
      {
        "id": 2,
        "name": "名字2"
      },
      {
        "id": 3,
        "name": "名字3"
      }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

JsonData实现的接口如下

JsonData data = JsonMapper.ToObject(jsondata);
//JsonData是用来接受JsonMapper解析json的数据
  • 1
  • 2

JsonMapper

string jsondata=JsonMapper.ToJson(obj);
//将对象obj转化为Json字符串
JsonData data = JsonMapper.ToObject(jsondata);
//将字符串转化为Json对象
//可以像键值对一样获取数据,即以中括号[]形式获取
JsonData jd=data[“key”]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

创建Json类型

public class Hero
    {
        public string name;
        public int power;
    }

    public class Heros
    {
        public Hero[] heros;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

第一种 创建Json数据

{'heros':[{'name':'Gary','power':90},{'name':'Gary2','power':80}]}
        Hero hero1 = new Hero();
        hero1.name = "Gary";
        hero1.power = 90;
        Hero hero2 = new Hero();
        hero2.name = "Gary2";
        hero2.power = 80;
        Heros heros = new Heros();
        heros.heros = new Hero[] { hero1, hero2 };
        string jsonStr = JsonMapper.ToJson(heros);
        Debug.Log(jsonStr);
        //解析Json
        Heros newHeros = JsonMapper.ToObject<Heros>(jsonStr);
        Debug.Log(newHeros.heros[0].name);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

第二种 创建Json数据

  //简单类型:{'name':'Gary','power':90}
        JsonData demo = new JsonData();
        //demo.SetJsonType(JsonType.Object);
        demo["name"] = "Gary";
        demo["power"] = 90;
        Debug.Log(demo.ToJson());
        //复杂类型:{'heros':[{'name':'Gary','power':90},{'name':'Gary2','power':80}]}
        JsonData demos = new JsonData(); //{}
        JsonData demo1 = new JsonData();
        demo1["name"] = "Gary1";
        demo1["power"] = 90;
        JsonData demo2 = new JsonData();
        demo2["name"] = "Gary2";
        demo2["power"] = 80;
        demos.SetJsonType(JsonType.Array);
        demos.Add(demo1);
        demos.Add(demo2);
        Debug.Log(demos.ToJson());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

解析Json

Heros newHeros = JsonMapper.ToObject<Heros>(jsonStr);
Debug.Log(newHeros.heros[0].name);

  • 1
  • 2
  • 3

以遍历的形式foreach

string jsonStr = "{'heros':[{'name':'Gary','power':90},{'name':'Gary2','power':80}]}";
        JsonData demos = JsonMapper.ToObject(jsonStr);
        JsonData heros = demos["heros"];  //[]
        //heros[1]["name"]
        foreach(JsonData heroJd in heros)
        {
            Debug.Log(heroJd["name"].ToString());
            Debug.Log((int)heroJd["power"]);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

///

/// <summary>
    /// 生成txt
    /// </summary>
    /// <param name="name">txt名字</param>
    /// <param name="strB">文本内容</param>
    private void ToTxt(string name, StringBuilder strB)// IList<string> content)
    {
        StreamWriter writer = null;
        try
        {
            lock (this)
            {
                // 检查文件夹
                string folderPath = Application.dataPath + "/Resources/Data";
                //string folderPath = @"C:\Users\Administrator\Desktop\InfoSystemDatas";
                if (!Directory.Exists(folderPath))
                {
                    //创建文件夹
                    Directory.CreateDirectory(folderPath);
                }
                //if (Directory.Exists(folderPath))
                //{
                //    //存在/成功创建 文件夹
                //    folderPath = folderPath + @"\";
                //}
                //else
                //{
                //    //无则当前路径创建文件
                //    folderPath = folderPath + @"-";
                //}
                //写入日志 
                string filePath = string.Format(folderPath + @"/{0}.txt", name);//, DateTime.Now.ToString("yyyy-MM-dd"));
                                                                               //设置覆盖写入
                FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                writer = new StreamWriter(fs);
                writer.Write(strB);
                //writer.WriteLine("\r\n----------------------------end----------------------------");
                writer.Dispose();
                UnityEditor.AssetDatabase.Refresh();
            }
        }
        catch (Exception e)
        {
           Debug.LogError(e.Message);
        }
        finally
        {
            if (writer != null)
            {
                writer.Close();
            }
        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/123066
推荐阅读
相关标签
  

闽ICP备14008679号