[英]Using SimpleJSON C# help getting info
我正在嘗試讀取json文件並將元素添加到列表中,這樣我可以更輕松地調用它們。 我無法弄清楚如何將所有“ Heads”值添加到列表中。
所以我可能會打電話
listname[index].key
listname[key].value
返回我可以得到示例的東西。(“ Small_Cube_Head”:{“ Colors”:{“ Default”})
我基本上希望能夠調用“ Small_Cube_Head”和所有顏色值
JSON文件
{
"Heads":{
"Large_Cube_Head":["Default","e97451"],
"Medium_Cube_Head":["Default","ffffff","76d7ea","e97451","5fa777","f4c430","d0ff14","0047ab","e32636","fc419a","720b98","ff8b00","000000","848482"],
"Small_Cube_Head":["Default"]
}
}
}
碼
/**
* json: Returns the whole file as a String
**/
private void LoadJson()
{
using (StreamReader r = new StreamReader("Assets/JSON/PlayerPartsList.json"))
{
json = r.ReadToEnd();
//Debug.Log(json);
JSONNode node = JSON.Parse(json);
Debug.Log(node["Heads"].Count); //returns 3
for (int i = 0; i < node["Heads"].Count; i++) {
//headParts.Add(node["Heads"].);
//Debug.Log(node["Heads"][i].Value.ToString());
//Debug.Log(node["Heads"]["Small_Cube_Head"].Value);
}
}
}
使用像枚舉器之類的Keys
來獲取每個標題名稱,然后可以循環瀏覽該headName的計數作為索引
KeyEnumerator headNameEnum = node["Heads"].Keys;
while (headNameEnum.MoveNext())
{
String headName = headNameEnum.Current().Value;
Debug.Log("headName: " + headName);
for (int i=0; i < node["Heads"][headName].Count; i++) {
String valueName = node["Heads"][headName][i].Value;
Debug.Log("valueName: " + valueName);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.