在创建库存系统时在Unity中出现奇怪错误

时间:2018-05-27 08:21:51

标签: c# json unity3d

所以我想创建一个库存系统,我已编码并完成了一些事情。但是现在它在项目数据库代码中显示错误。错误是:

  

KeyNotFoundException:字典中没有给定的键。   System.Collections.Generic.Dictionary 2[System.Int32,System.Collections.Generic.IDictionary 2 [System.Int32,System.Int32 []]]。get_Item(Int32 key)(在/ Users / builduser / buildslave / mono / build / mcs / class / corlib / System.Collections.Generic / Dictionary.cs:150)   LitJson.JsonReader.Read()   Rethrow as JsonException:输入字符串中的标记'123'无效   LitJson.JsonReader.Read()   LitJson.JsonMapper.ReadValue(LitJson.WrapperFactory工厂,LitJson.JsonReader读者)   LitJson.JsonMapper.ReadValue(LitJson.WrapperFactory工厂,LitJson.JsonReader读者)   LitJson.JsonMapper.ReadValue(LitJson.WrapperFactory工厂,LitJson.JsonReader读者)   LitJson.JsonMapper.ToWrapper(LitJson.WrapperFactory工厂,System.String json)   LitJson.JsonMapper.ToObject(System.String json)   ItemDatabase.Start()(在Assets / All / Scripts / ItemDatabase.cs:13)

因为我无法弄清楚如何修复它而变得非常烦人。我目前的2个文件是:

using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine;
using System.IO;

public class ItemDatabase : MonoBehaviour {
    private List<Item> database = new List<Item>();
    private JsonData itemData;

void Start()
{
    itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
    ConstructItemDatabase();

    Debug.Log(database[1].Slug);
}

void ConstructItemDatabase()
{
    for (int i = 0; i < itemData.Count; i++)
    {
        database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"],
            (int)itemData[i]["stats"]["power"], (int)itemData[i]["stats"]["defence"], (int)itemData[i]["stats"]["vitality"], itemData[i]["description"].ToString(),
            (bool)itemData[i]["stackable"], (int)itemData[i]["rarity"], 
itemData[i]["slug"].ToString()));
        }
    }
}

public class Item
{
    public int ID { get; set; }
    public string Title { get; set; }
    public int Value { get; set; }
    public int Power { get; set; }
    public int Defence { get; set; }
    public int Vitality { get; set; }
    public string Description { get; set; }
    public bool Stackable { get; set; }
    public int Rarity { get; set; }
    public string Slug { get; set; }

public Item(int id, string title, int value, int power, int defence, int vitality, string description, bool stackable, int rarity, string slug)
{
    this.ID = id;
    this.Title = title;
    this.Value = value;
    this.Power = power;
    this.Defence = defence;
    this.Vitality = vitality;
    this.Description = description;
    this.Stackable = stackable;
    this.Rarity = rarity;
    this.Slug = slug;
}

public Item()
{
    this.ID = -1;
    }

}

这里最上面的是我的ItemDatabase.cs文件。它有一些空格和括号错误我知道,但它实际上都很好。我将发布整个代码的截图。由于我不熟悉使用此代码示例工具。

这是我的Items.json文件。

[
{
    "id": 0,
    "title": "Wood Pickaxe",
    "value": 6,
    "stats" {
      "power": 1,
      "defence": 0,
      "vitality": 0
    },
    "description": "A basic wood pickaxe.",
    "stackable": false,
    "rarity": 1,
    "slug": "wood_pickaxe"
},
{
    "id": 1,
    "title": "Wood Pickaxe Head",
    "value": 543,
    "stats" {
      "power": 1,
      "defence": 0,
      "vitality": 0
        },
        "description": "A piece of a broken wood pickaxe.",
        "stackable": false,
        "rarity": 1,
        "slug": "wood_pickaxe_head"
    }
]

我还会发布此代码的屏幕截图。请帮忙。这非常令人沮丧,关于此的其他帖子也无济于事。 Items.json screenshot ItemDatabase.cs screenshot

1 个答案:

答案 0 :(得分:1)

你的json中有错误。错误本身有点误导。每次我看到它时,都会涉及到json语法问题。

[
    {
        "id": 0,
        "title": "Wood Pickaxe",
        "value": 6,
        "stats": { // <-- problem was here. missing colon
            "power": 1,
            "defence": 0,
            "vitality": 0
        },
        "description": "A basic wood pickaxe.",
        "stackable": false,
        "rarity": 1,
        "slug": "wood_pickaxe"
    },
    {
        "id": 1,
        "title": "Wood Pickaxe Head",
        "value": 543,
        "stats": { // <-- problem was here. missing colon
            "power": 1,
            "defence": 0,
            "vitality": 0
        },
        "description": "A piece of a broken wood pickaxe.",
        "stackable": false,
        "rarity": 1,
        "slug": "wood_pickaxe_head"
    }
]