【发布时间】:2019-10-03 08:52:56
【问题描述】:
我正在尝试获取数组“channels”中的项目数,该数组位于名为“modes”的数组中。我已经很容易从“modes”数组中的正确项中获取名称,但现在我需要获取“channels”数组中的项数。
“modes”数组中的每个“name”都在一个名为“cbxModes”的组合框中。我试过有一个if语句说“name”等于“cbxModes.SelectedItem”,然后从“modes”中的这个项目获取“channels”中的项目数
这是我的 JSON
"modes": [
{
"name": "1 – Standard 16bit RGBW",
"shortName": "1-16bitRGBW",
"channels": [
"Pan",
"Pan fine",
"Tilt",
"Tilt fine",
"Pan/Tilt Speed",
"Special Functions",
"Virtual Color Wheel",
"Red",
"Red fine",
"Green",
"Green fine",
"Blue",
"Blue fine",
"White",
"White fine",
"CTC",
"Color Mix Control",
"Zoom",
"Zoom fine",
"Shutter / Strobe",
"Dimmer",
"Dimmer fine"
]
},
{
"name": "2 – Reduced 8bit RGBW",
"shortName": "2-8bitRGBW",
"channels": [
"Pan",
"Pan fine",
"Tilt",
"Tilt fine",
"Pan/Tilt Speed",
"Special Functions",
"Virtual Color Wheel",
"Red",
"Green",
"Blue",
"White",
"CTC",
"Color Mix Control",
"Zoom",
"Dimmer"
]
},
{
"name": "1 – Standard 16bit CMY",
"shortName": "1-16bitCMY",
"channels": [
"Pan",
"Pan fine",
"Tilt",
"Tilt fine",
"Pan/Tilt Speed",
"Special Functions",
"Virtual Color Wheel",
"Cyan",
"Cyan fine",
"Magenta",
"Magenta fine",
"Yellow",
"Yellow fine",
null,
null,
"CTC",
"Color Mix Control",
"Zoom",
"Zoom fine",
"Shutter / Strobe",
"Dimmer",
"Dimmer fine"
]
},
{
"name": "2 – Reduced 8bit CMY",
"shortName": "2-8bitCMY",
"channels": [
"Pan",
"Pan fine",
"Tilt",
"Tilt fine",
"Pan/Tilt Speed",
"Special Functions",
"Virtual Color Wheel",
"Cyan",
"Magenta",
"Yellow",
null,
"CTC",
"Color Mix Control",
"Zoom",
"Shutter / Strobe",
"Dimmer"
]
}
],
这是我的 C#
private void CbxMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string FixtureRoot = AppDomain.CurrentDomain.BaseDirectory + "Fixtures\\" + cbxManufacture.SelectedItem + "\\";
string ModeRoot = null;
var JSON_Mode_Count = (JObject)null;
for (int i = 0; i < Directory.GetFiles(FixtureRoot).Length; i++)
{
var Fixtures = Directory.GetFiles(FixtureRoot);
string Names = File.ReadAllText(Fixtures[i]);
var JSON_Name = JsonConvert.DeserializeObject<dynamic>(Names);
if (JSON_Name.name == cbxFixture.SelectedItem)
{
ModeRoot = AppDomain.CurrentDomain.BaseDirectory + "Fixtures\\" + cbxManufacture.SelectedItem + "\\" + JSON_Name.fixtureKey.ToString() + ".json";
JSON_Mode_Count = JObject.Parse(File.ReadAllText(ModeRoot));
JArray Modes = (JArray)JSON_Mode_Count["modes"];
int numberOfModes = Modes.Count;
string ModesJSON = File.ReadAllText(ModeRoot);
var JSON_Mode = JsonConvert.DeserializeObject<dynamic>(ModesJSON);
foreach (var obj in JSON_Mode.modes)
{
foreach (var obj2 in obj.channels)
{
if (obj.name == cbxMode.SelectedItem)
{
JArray Channels = (JArray)JSON_Mode_Count["channels"];
int numberOfChannels = Channels.Count;
lblChannels.Content = numberOfChannels.ToString();
}
}
}
}
}
}
TIA
【问题讨论】:
-
如果在 C# 中有一个 JSON 模型,您可以将 JSON 反序列化到该模型,这将更容易理解(并且您可以开发)
-
这个问题应该使用适当的对象和反序列化自行解决。
-
太棒了。谢谢。介意扩展您的意思吗?
-
好的。 1.复制你的Json。 2. 在 Visual Studio 上,创建一个新类。 3.在编辑菜单中点击特殊过去“过去的json作为类”。 4.使用
JsonConvert.DeserializeObject<RootObject>(json);现在你应该有一个真实的对象导航会更容易。拥有真实对象意味着您将轻松浏览该属性 -
完成后,您将更清楚地了解您接下来遇到的问题。因为在动态类型对象上,我要求一个不存在的任何类型的属性,并且不知道它不是真实的。这不会解决您的问题,它会帮助您找到问题所在