嘿伙计们在教程中使用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次。
感谢任何帮助 感谢
答案 0 :(得分:4)
你的班级应该是这样的
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; }
}
因为&#34;权力&#34;,&#34;防御&#34;,&#34;活力&#34;在&#34; stats&#34;。
其次,我建议您使用Newtonsoft将这种JSON转换为对象。我相信这很容易理解。