c# - 使用 SimpleJSON C# 帮助获取信息
问题描述
- 不要说Unity Json里面有内置或者下载Lindq for C# 我用的是SimpleJSON
我正在尝试读取 json 文件并将元素添加到列表中,以便我可以更轻松地调用它们。我无法弄清楚如何将所有“Heads”值添加到列表中。
所以我可以打电话
listname[index].key
listname[key].value
返回一些我可以获得示例的东西。(“Small_Cube_Head”:{“Colors”:{“Default”})
我基本上希望能够调用“Small_Cube_Head”和所有颜色值
文件
{
"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
来获取每个头部名称,然后您可以循环遍历该头部名称的计数作为索引
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);
}
}
推荐阅读
- dgrid - dgrid 从 0.3 更新 - 值得吗?
- http - 用于大字符串有效负载的 HTTP GET
- javascript - 有没有办法在 Java Script 中从 WSDL 生成 Web 服务?
- sql - TOAD 数据点 Postgres SQL 字符串输出被截断
- forms - RenderDecoratedBox 需要合成尺寸:缺失
- javascript - Angular Reactive forms从指令验证控件
- javascript - 更改可见性后不显示元素
- laravel - Laravel Sail 无法使用 ubuntu:20.04 构建
- java - 如果其他一些服务在 Java Spring Boot 中更新了数据库,如何在几分钟后刷新缓存?
- node.js - 使用netlify cms创建博客时如何在markdown文件中动态获取slug?