用Litjson解析,命名空间我弄好了,但是foreach那行的时候报错了: Instance of JsonData is not a dictionary这个是什么鬼?

花生

2 回复26 浏览

来自课程:背包系统 装备系统和锻造系统 - 基于Unity5.3UGUI

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class InventoryManager : MonoBehaviour
{
    #region 单例模式
    private static InventoryManager _instance;
    public static InventoryManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("Manager").GetComponent<InventoryManager>();
            }
            return _instance;
        }
    }
    #endregion
    private List<Item> itemList;
    void Start()
    {
        ParseItemJson();
    }
    void ParseItemJson()
    {
        itemList = new List<Item>();
        TextAsset t = Resources.Load<TextAsset>("Comsumable");
        JsonData jsonDate = JsonMapper.ToObject(t.text);
        foreach (JsonData temp in jsonDate)
        {
            print("ss");
            JsonData idValue = temp["id"];
            JsonData nameValue = temp["name"];
            JsonData iconValue = temp["icon"];
            JsonData typeValue = temp["type"];
            JsonData qualityValue = temp["quality"];
            JsonData maxStackValue = temp["maxStack"];
            JsonData descriptionValue = temp["description"];
            JsonData buyPriceValue = temp["buyPrice"];

 int id = int.Parse(idValue.ToString());
            string name = nameValue.ToString();
            string icon = iconValue.ToString();
            Item.Itemtype type = (Item.Itemtype)System.Enum.Parse(typeof(Item.Itemtype),typeValue.ToString());
            Item.ItemQuiality quality = (Item.ItemQuiality)System.Enum.Parse(typeof(Item.ItemQuiality), qualityValue.ToString());
            int maxStack = int.Parse(maxStackValue.ToString());
            string description = descriptionValue.ToString();
            int buyPrice = int.Parse(buyPriceValue.ToString());
            int sellPrice = int.Parse(sellPriceValue.ToString());

            Item item = null;
            switch (type)
            {
                case Item.Itemtype.Consumable:
                    JsonData hpValue = temp["hp"];
                    JsonData mpValue = temp["mp"];
                    int hp = int.Parse(hpValue.ToString());
                    int mp = int.Parse(mpValue.ToString());
                    item = new Consumable(id, name, icon, type, quality, description, maxStack, buyPrice, sellPrice, hp, mp);
                    break;
                case Item.Itemtype.Equipment:
                    break;
                case Item.Itemtype.Weapon:
                    break;
                case Item.Itemtype.Material:
                    break;
                default:
                    break;
            }

全部回复(2)

  • siki 1 楼

    该用for进行遍历 for(int i ;;) 你需要取得jsondata的长度,在c#的第四季里面也有讲解的哈

  • siki 2 楼

    以后提问 选择问答类型,不是问答类型,老师收不到提示

发表回复

登录后可以回复