1

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"}
            ]
        }
}

1 Answer 1

2

You just need to include ToString() in your Select expression after x.Value:

JObject originalObject = JObject.Parse(json);
var analogInputTrueValues = originalObject.Descendants()
                                          .OfType<JProperty>()
                                          .Where(p => p.Name == "AnalogInput")
                                          .Select(x => x.Value.ToString())
                                          .ToArray();

Working example: https://dotnetfiddle.net/tU5Mc8

Alternative method using strongly-typed classes: https://dotnetfiddle.net/US4Bs0

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked. Is the way that I am extracting the AnalogInput value a good way? Is there a better way to do it?
Using LINQ-to-JSON, the way you are doing, is correct and good. An alternative method would be to create strongly-typed classes corresponding to your JSON and then use JsonConvert.DeserializeObject() to deserialize the JSON into your classes. It comes down to personal preference.
I added some fiddles to my answer so you can compare the difference between the two methods. Using LINQ-to-JSON is much less code, but some people find the strongly typed classes easier to work with.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.