Unity 关于json数据的解析方式(LitJson.dll插件)

关于json数据的解析方式(LitJson.dll插件)

   void ParseItemJson()
   {
   
       
       TextAsset itemText = Resources.Load<TextAsset>("Items");//读取Resources中Items文件,需要将Items文件放到Resources文件夹中
       string itemJson = itemText.text;//读取Json信息
       JsonData s=JsonMapper.ToObject(itemJson);//声明解析数组
       // Debug.Log(itemJson);
       // Debug.Log("ID:"+s[0]["id"]+ " " + "名称:"+ s[0]["name"]+" "+"简介:"+ s[0]["description"]);//因为是数据组,所以要加[0]指定层级
       // Debug.Log("ID:" + s[1]["id"] + " " + "名称:" + s[1]["name"] + " " + "简介:" + s[1]["description"]);
      //遍历Json数组
       foreach (JsonData temp in s) {
   

           Debug.Log(temp["name"].ToString() + temp["description"] + temp["type"]);
       }

   }

一个简单的解析方法,使用LitJson.dll插件,注意的是,如果数据是一个数组,单独使用时需要需要增加序号,如程序中的 s[0][“id”] ,0就是第几个数组,遍历则不需要,下边是Json文件内容,有兴趣可以试试。

[
  {
   
    "id": 1,
    "name": "血药",
    "description": "这玩意能加血。",
    "type": "Consumable",
    "capacity": 10,
    "buyPrice": 10,
    "sellPrice": 5,
    "hp": 10,
    "mp": 0,
    "sprite": "Resources/Sprites/Items/hp.png"
  },
  {
   
    "id": 2,
    "name": "蓝药",
    "description": "这玩意能加蓝。",
    "type": "Consumable",
    "capacity": 10,
    "buyPrice": 10,
    "sellPrice": 5,
    "hp": 0,
    "mp": 10,
    "sprite": "Resources/Sprites/Items/mp.png"
  }
]

相关推荐

  1. Unity 关于json数据解析方式(LitJson.dll

    2023-12-29 21:10:04       57 阅读
  2. Unity数据解析Json、XML、CSV、二进制)

    2023-12-29 21:10:04       56 阅读
  3. 解决Unity WebGLInput全屏输入问题

    2023-12-29 21:10:04       56 阅读
  4. vscode中好用json

    2023-12-29 21:10:04       27 阅读
  5. Unity DOTween常用方法(一)

    2023-12-29 21:10:04       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 21:10:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 21:10:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 21:10:04       87 阅读
  4. Python语言-面向对象

    2023-12-29 21:10:04       96 阅读

热门阅读

  1. PageHelper 使用示例

    2023-12-29 21:10:04       58 阅读
  2. 09-网络安全框架及模型-(SSE-CMM)

    2023-12-29 21:10:04       41 阅读
  3. Android APP耗电量采集数据模板

    2023-12-29 21:10:04       55 阅读
  4. 在 Python 中编写循环Loops的艺术

    2023-12-29 21:10:04       64 阅读
  5. 算法练习Day22 (Leetcode/Python-回溯算法)

    2023-12-29 21:10:04       53 阅读
  6. Flask 密码重设系统

    2023-12-29 21:10:04       57 阅读
  7. python高级代码

    2023-12-29 21:10:04       56 阅读
  8. 6、LLaVA

    6、LLaVA

    2023-12-29 21:10:04      58 阅读