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

(196评价)
价格: 4069.00元
背包系统使用jsonMapper去解析
冫爻刂发起了问答2016-12-28
2
回复
4360
浏览

代码如下

void ParseItemJson ()
    {
        itemList = new List<Item> ();
        //文本在Unity中为TextAsset类型
        TextAsset itemText = Resources.Load<TextAsset> ("Items");
        string itemJson = itemText.text;//物品信息的json格式
//        Debug.Log (itemJson);     是可以读取到json内容的
        //使用jsonMapper去解析json文本
        JsonData jsonData = JsonMapper.ToObject(itemJson);
        foreach (JsonData temp in jsonData) {
            JsonData id = temp ["id"];
            int i = (int)id;
            Debug.Log (i);
        }

    }

在start方法调用该方法,然后报错为

InvalidOperationException: Instance of JsonData is not a dictionary
LitJson.JsonData.EnsureDictionary ()
LitJson.JsonData.System.Collections.Specialized.IOrderedDictionary.GetEnumerator ()
LitJson.JsonData.System.Collections.IDictionary.GetEnumerator ()
InventoryManager.ParseItemJson () (at Assets/Scripts/InventoryManager.cs:43)
InventoryManager.Start () (at Assets/Scripts/InventoryManager.cs:31)

但是jsondata本就是一个数组,为什么用foreach循环遍历无法读取呢?

所有回复
发表回复
你还没有登录,请先 登录或 注册!