I'm trying to get the number of items in array "channels" which is inside an array called "modes". I've been easily able to get the name from the correct item in the "modes" array but now I need to get the number of items in the "channels" array.
Each "name" from the "modes" array is in a combobox names "cbxModes". I've tried having an if statement saying that is "name" is equal to "cbxModes.SelectedItem" then get number of items in "channels" from this item in "modes"
Here is my 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"
]
}
],
This is my 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
JsonConvert.DeserializeObject<RootObject>(json);Now you should have a real object navigating will be easier. And Having real object mean you will easly navigate the property