Unity3D_(数据)LitJson创建和解析Json

 

 

  LitJson github:  传送门

  JsonUtility创建和解析Json  传送门

  LitJson.dll百度云盘  传送门  密码:p1py  

  

  加载LitJson.dll到Unity中

  在Assets文件夹下创建一个Plugins文件(文件名不得有错),把LitJson.dll放进去

  新建一个LitJson_Gary.cs脚本,挂在到GameObject上

 

 

第一种使用LitJson创建和解析Json

  好处:逻辑清晰,适用于稍微复杂的Json类型,适用于层级复杂情况

  坏处:需要单独写类,写的代码稍微多一些

 

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

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

public class Heros
{
    public Hero[] heros;
}

public class LitJson_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        func1();
    }
    
    void func1()
    {
        //创建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);

    }
}
LitJson_Gary.cs

 

  创建Json类型

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

    public class Heros
    {
        public Hero[] heros;
    }

 

  创建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);

 

 

第二种使用LitJson创建和解析Json

  好处:代码简单,适用于层级简单情况

  坏处:当层级比较复杂的时候,比较容易出错

 

  创建Json

 

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

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

public class Heros
{
    public Hero[] heros;
}

public class LitJson_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        fun2();
    }
    
    void fun2()
    {
        //创建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());

    }

    void fun3()
    {
        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"]);
        }

    }

}
LitJson_Gary.cs

 

 

  解析Json

 

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

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

public class Heros
{
    public Hero[] heros;
}

public class LitJson_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        fun3();
    }
    

    //解析Json
    void fun3()
    {
        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"]);
        }

    }

}
LitJson_Gary.cs

 

posted @ 2018-11-12 20:36  Cynical丶Gary  阅读(4805)  评论(0编辑  收藏  举报