关于解析json笔记

例子1:
json文件内容
在这里插入图片描述

[
{
“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的区别是没有了枚举类型,解析时使用另一种方法解析
在这里插入图片描述

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

例子3 解析 数组中包含数组的 json数据
在这里插入图片描述

[
{
    "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<Item3[]>(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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值