gpt4 book ai didi

c# - 未找到 key 异常 - Unity Json

转载 作者:太空宇宙 更新时间:2023-11-03 20:56:14 29 4
gpt4 key购买 nike

大家好,在教程中使用 Json 为 Unity 实现以下数据库时遇到问题,搜索了几个小时但似乎无法找到问题

这是我的代码:ItemDatabase.cs

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

void Start()
{
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.Json")); // change from json object to be readible by c#
ConstructItemDatabase();

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

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]["stats"]["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 Defence { get; set; }
public int Power { 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.Defence = defence;
this.Power = power;
this.Vitality = vitality;
this.Description = description;
this.Stackable = stackable;
this.Rarity = rarity;
this.Slug = slug;

}

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

和我的 json 文件 Items.json 的代码

[
{
"id": 0,
"title": "Steel Gloves",
"value": 6,
"stats": {
"power": 1,
"defence": 4,
"vitality": 2
},
"description": "Gloves with steel plating",
"stackable": false,
"rarity": 2,
"slug": "steel_gloves"
},
{
"id": 1,
"title": "The Great Stick",
"value": 543,
"stats": {
"power": 56,
"defence": 2,
"vitality": 54
},
"description": "A Stick that's pretty great",
"stackable": true,
"rarity": 6,
"slug": "the_great_stick"
}
]

当我从 visual studio 调试程序时,它工作得很好,但是在 Unity 中,当我点击播放时,我遇到了一些错误:

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,LitJson.JsonData].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
LitJson.JsonData.get_Item (System.String prop_name)
ItemDatabase.ConstructItemDatabase () (at Assets/Scripts/ItemDatabase.cs:27)
ItemDatabase.Start () (at Assets/Scripts/ItemDatabase.cs:15)

我以为我在 ContructItemDatabase() 中拼错了一个字符串,但我觉得我已经检查了 100 遍了。

感谢任何帮助谢谢

最佳答案

你的类应该是这样的

public class Stats
{
public int power { get; set; }
public int defence { get; set; }
public int vitality { get; set; }
}

public class items
{
public int id { get; set; }
public string title { get; set; }
public int value { get; set; }
public Stats stats { get; set; }
public string description { get; set; }
public bool stackable { get; set; }
public int rarity { get; set; }
public string slug { get; set; }
}

因为“力量”、“防御”、“活力”在“统计”之下。

其次我会推荐你​​使用Newtonsoft将这种 JSON 转化为对象。我相信这很容易理解。

关于c# - 未找到 key 异常 - Unity Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50385905/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com