1、LitJson下载
百度云
链接:https://pan.baidu.com/s/1BmmqLJ5asX2DSDxfR29KLQ
提取码:80gl
2、LitJson如何使用
Json文本内容 Items
[
{
"id": 1,
"name": "血瓶",
"type": "Consumable",
"sprite": "Sprites/Items/hp"
},
{
"id": 2,
"name": "长剑",
"type": "Weapon",
"sprite": "Sprites/Items/Saber"
}
]
对应Json的物品类
public class Item
{
///
/// 物品ID
///
public int ID { get; set; }
///
/// 物品名字
///
public string Name { get; set; }
///
/// 物品类型
///
public ItemType Type { get; set; }
///
/// 物品资源地址
///
public string Sprite { get; set; }
public Item(int id , string name,ItemType type,string sprite)
{
this.ID = id;
this.Name = name;
this.Type = type;
this.Sprite = sprite;
}
///
/// 物品类型
///
public enum ItemType
{
Consumable, //消耗品
Weapon //装备
}
}
解析Json类 Items
public class JsonTest : MonoBehaviour
{
private List- database = new List
- ();
void Start()
{
ParseItemJson();
}
///
/// 解析物品信息
///
private void ParseItemJson()
{
//获取Json中的文本。文本在unity中是textasset类型
TextAsset itemText = Resources.Load("Items");
//把json文本转换为jsondata格式
JsonData itemData = JsonMapper.ToObject(itemText.text);
//对每一个物品,都新建个item类来存储
for (int i = 0; i < itemData.Count; i++)
{
int id = (int)itemData[i]["id"];
string name = (string)itemData[i]["name"];
//枚举类型则要把字符串转换为枚举类型
//用System.Enum.Parse方法
Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), itemData[i]["type"].ToString());
string sprite = (string)itemData[i]["Sprite"];
Item item = new Item(id, name, type,sprite);
database.Add(item);
}
Debug.Log(itemList[1]);
}
}