Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(209评价)
价格: 3620.00元
背包系统中用litjson解析出的问题
圆周率骑士发起了问答12-10
3
回复
113
浏览

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

public class InventroyManager : MonoBehaviour
{

    #region 单例模式
    private static InventroyManager _instance;
    public static InventroyManager Instance
    { 
        
         
     get 
        { 
        
            if(_instance==null)
            {
                //下面的代码只会执行一次
                _instance=GameObject.Find("InventroyManager").GetComponent<InventroyManager>();
            }
        
            return _instance; 

        }
    }
    #endregion

    private List<Item> itemslist;

    private void Start()
    {
        ParseItemJson();
    }

    private void ParseItemJson()
    {
        itemslist = new List<Item>();
        //获取Json文件中的内容
        TextAsset itemAsset = Resources.Load<TextAsset>("Json/Item");
        string itemjson= itemAsset.text;

        //转化为JsonData类型呢的数据
        //JsonData itemdata = JsonMapper.ToObject(itemjson);
        itemslist = JsonMapper.ToObject< List<Item>> (itemjson);
 
        Debug.Log(itemslist[0].ID);


        //循环遍历其中JsonData的数据并转化为相应的数据类型
        //foreach (JsonData temp in itemdata)
        //{
           
        //    //获取Item的类型
        //    string typestr = temp["type"].ToString();
        //    //通过字符串枚举类型,转化成实际枚举类型
        //    Item.ItemType type= (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typestr);

        //    //共有属性解析
        //    int id = (int)temp["id"];
        //    string name = temp["name"].ToString();
        //    Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality), temp["quality"].ToString());
        //    string description = temp["description"].ToString();
        //    int capacity = (int)temp["capacity"];
        //    int buyprice= (int)temp["buyprice"];
        //    int sellprice = (int)temp["sellprice"];
        //    string sprite = temp["sprite"].ToString();

        //    Item item = null;
        //    switch (type)
        //    {
        //        case Item.ItemType.Consumable:
        //            //解析特有属性
                    
        //            int hp = (int)temp["hp"];
        //            int mp = (int)temp["mp"];
        //            item = new Consumable(id,name,type,quality,description,capacity,buyprice, sellprice, sprite,hp,mp);
        //            break;
        //        case Item.ItemType.Equipment:
        //            break;
        //        case Item.ItemType.Weapon:
        //            break;
        //        case Item.ItemType.Material:
        //            break;
               
        //    }
        //    //将道具对象存入列表
        //    itemslist.Add(item);
        //    Debug.Log(item);
        //}

    }

}

解析的时候我使用了litjson,为什么JsonMapper.ToObject< List<Item>> (itemjson);解析出的对象没有办法Debug.Log(itemslist[0].ID);打印出属性值。

所有回复
  • 老师_小面包 12-10

    同学你先转换成字符串,再次打印~

    • 圆周率骑士 12-10

      ArgumentException: The value passed in must be an enum base or an underlying type for an enum, such as an Int32.
      Parameter name: value
      System.Enum.ToObject (System.Type enumType, System.Object value) (at <1071a2cb0cb3433aae80a793c277a048>:0)
      LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) (at Assets/Plugins/LitJson/JsonMapper.cs:367)
      LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) (at Assets/Plugins/LitJson/JsonMapper.cs:450)
      LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) (at Assets/Plugins/LitJson/JsonMapper.cs:408)
      LitJson.JsonMapper.ToObject[T] (System.String json) (at Assets/Plugins/LitJson/JsonMapper.cs:931)
      InventroyManager.ParseItemJson () (at Assets/Scripts/Items/InventroyManager.cs:47)
      InventroyManager.Start () (at Assets/Scripts/Items/InventroyManager.cs:35)

      改过来还是会报这个错误

      (0) 回复
    • 老师_小面包 12-15

      回复 @ 圆周率骑士: 那就是litjson不兼容debug语句,换个json解析插件~

      (0) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!