I have a Json document and I am trying to get the value for AnalogInput for each channel from 1 to 4. I have tried this code:
JObject originalObject = JObject.Parse(testJsonObject);
var analogInputTrueValues = originalObject.Descendants().OfType<JProperty>().Where(p => p.Name == "DigitalInput").Select(x => x.Value).ToArray();
where testJsonObject is the Json file that gets loaded by another method.
Debugging the code, the value for analogInputTrueValues is:
{Newtonsoft.Json.Linq.JToken[4]}
[0]: {13}
[1]: {13}
[2]: {14}
[3]: {14}
,which is correct. but I am interested to have an array or a list like {"13","13","14","14"}. This is where I can not move forward since I can not extract those exact values and have them in a list or an array. Even when I do:
digitalInputTrueValues.GetValue(0)
{13}
base: {13}
HasValues: false
Type: String
Value: "13"
I can't extract the Value, which is what I am interested in. How can I get around this kind of problem and extract my desired values? The object that I am working with is as follows:
{
"module": {
"serial": "3",
"label": "A",
"lat": "B",
"long": "C",
"channels": [
{"channel": "1", "label": "Channel 1", "AnalogInput": "13", "AnalogInputRaw": "13", "AnalogInputScale": "Raw", "DigitalInput": "Off"},
{"channel": "2", "label": "Channel 2", "AnalogInput": "13", "AnalogInputRaw": "13", "AnalogInputScale": "Raw", "DigitalInput": "On"},
{"channel": "3", "label": "Channel 3", "AnalogInput": "14", "AnalogInputRaw": "14", "AnalogInputScale": "Raw", "DigitalInput": "On"},
{"channel": "4", "label": "Channel 4", "AnalogInput": "14", "AnalogInputRaw": "14", "AnalogInputScale": "Raw", "DigitalInput": "On"}
],
"variables": [
{"1": "0"},
{"2": "0"},
{"3": "1"},
{"4": "0"}
]
}
}