关于解析json笔记

例子1:
json文件内容
关于解析json笔记_第1张图片

[
{
“id”: 1,
“name”: “血瓶”,
“type”: “Consumable”,
“quality”: “Common”,
“description”: “这个是用来加血的”,
“capacity”: 10,
“buyprice”: 10,
“sellprice”: 5,
“hp”: 10,
“mp”: 0,
“sprite”: “Sprites/Items/hp”
}
]

首先引入LitJson.dll文件
然后使用下面代码即可进行解析 int string 枚举都可以解析

    JsonData jsondata = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/json/info.json"));
    foreach (JsonData item in jsondata)
    {
        JsonData idValue = item["id"];
        JsonData nameValue = item["name"];
        JsonData typeValue = item["type"];
        JsonData qualityValue = item["quality"];
        JsonData descriptionValue = item["description"];
        JsonData capacityValue = item["capacity"];
        JsonData buypriceValue = item["buyprice"];
        JsonData sellpriceValue = item["sellprice"];
        JsonData hpValue = item["hp"];
        JsonData mpValue = item["mp"];
        JsonData spriteValue = item["sprite"];
        int id = int.Parse(idValue.ToString());
        string name = nameValue.ToString();
        Item.itemType type = (Item.itemType)System.Enum.Parse(typeof(Item.itemType), typeValue.ToString());
        Item.Quality quality = (Item.Quality)System.Enum.Parse(typeof(Item.Quality), qualityValue.ToString());
        string description = descriptionValue.ToString();
        int capacity = int.Parse(capacityValue.ToString());
        int buyprice = int.Parse(buypriceValue.ToString());
        int sellprice = int.Parse(sellpriceValue.ToString());
        int hp = int.Parse(hpValue.ToString());
        int mp = int.Parse(mpValue.ToString());
        string sprite = spriteValue.ToString();

        Item itemObj = new Item();
        itemObj.id = id;
        itemObj.name = name;
        itemObj.type = type;
        itemObj.quality = quality;
        itemObj.description = description;
        itemObj.capacity = capacity;
        itemObj.buyprice = buyprice;
        itemObj.sellprice = sellprice;
        itemObj.hp = hp;
        itemObj.mp = mp;
        itemObj.sprite = sprite;
        Debug.Log(itemObj);
    }

Item的类。
public class Item
{
public int id
{ get; set; }
public string name
{ get; set; }
public itemType type
{
get;set;
}
public Quality quality
{
get;set;
}
public string description
{ get; set; }
public int capcity
{ get; set; }
public int buyprice
{ get; set; }
public int sellprice
{ get; set; }
public int hp
{ get; set; }
public int mp
{ get; set; }
public string sprite
{ get; set; }
public enum itemType
{
Consumable,
Equipment,
Weapon,
Material
}
public enum Quality
{
Common,
Uncommon,
Rare,
Epic,
Legendary,
Artifact,
}
public override string ToString()
{
return id+""+name+""+type+""+quality+""+description+""+capcity+""+buyprice+""+sellprice+""+hp+""+mp+""+sprite;
}
}

例子2:
json内容:
和例子1的区别是没有了枚举类型,解析时使用另一种方法解析
关于解析json笔记_第2张图片

[
{
“id”: 1,
“name”: “血瓶”,
“description”: “这个是用来加血的”,
“capacity”: 10,
“buyprice”: 10,
“sellprice”: 5,
“hp”: 10,
“mp”: 0,
“sprite”: “Sprites/Items/hp”
}
]

对应的类模型
public class Item2
{
public int id
{ get; set; }
public string name
{ get; set; }
public string description
{ get; set; }
public int capacity
{ get; set; }
public int buyprice
{ get; set; }
public int sellprice
{ get; set; }
public int hp
{ get; set; }
public int mp
{ get; set; }
public string sprite
{ get; set; }
public override string ToString()
{
return id + " " + name + " "+ description + " " + capacity + " " + buyprice + " " + sellprice + " " + hp + " " + mp + " " + sprite;
}
}

代码:
Item2[] items = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + “/json/info1.json”));
foreach (Item2 item in items)
{
Debug.Log(item);
}
这种解析比例子1要少写很多代码,但是解析枚举类型总是出错 我不知道为什么,没有找到对应的解决方法。

例子3 解析 数组中包含数组的 json数据
关于解析json笔记_第3张图片

[
{
    "id": 1,
    "name": "血瓶",
    "type": "Consuable",
    "quality": "Common",
    "description": "这个是用来加血的",
    "capacity": 10,
    "buyprice": 10,
    "sellprice": 5,
    "hp": 10,
    "mp": 0,
    "sprite": "Sprites/Items/hp",
     "skill":[
             { "id":10,"name":"天下无双"},
             { "id":11,"name":"天下无贼"}
             ]
}

]

因为数组中包含有数组,所以需要 在遍历一次
代码:

    Item3[] items = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/json/info2.json"));
    foreach (Item3 item in items)
    {
        Debug.Log(item);
    }
    for (int i = 0; i < items.Length; i++)
    {
        foreach (skill item in items[i].skill)
        {
            Debug.Log(item);
        }
    }

你可能感兴趣的:(关于解析json笔记)