2

Firstly, I know how to deserialize json into objects in general but I am still very new to C#. I am able to deserialize the json below, but the outcome is not what I would call "elegant".

{
  "slot":0,
    "io":{
      "relay":{
        "4":{
          "relayStatus":1
        }
      }
    }
 }

The issue I am having is with the relay number, "4" in this instance, as the data name in the object is an integer. The "4" could be any value from 0 to 5.

What I currently have coded is a C# object that I can reference like this..

relays.Io.Relay.Relay4.RelayStatus

Which is okay but I would like to be able to reference it like this..

relayStatus[4]

Which would be an array with a length of 6 that contains the value of "relayStatus" at position 4.

My apologies if I have not asked this question very well. Please feel free to ask for more explanation if required.

Serge
  • 40,935
  • 4
  • 18
  • 45
Mush
  • 33
  • 5
  • 1
    Does this answer your question? [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers), [Deserializing JSON with dynamic keys](https://stackoverflow.com/q/13517792/8017690) – Yong Shun Jun 24 '22 at 07:11
  • Just to clarify, in this particular example you would like to have an `relayStatus` array with length 6 which contains 1 `Relay` object and 5 `null`s. Is my understanding correct? – Peter Csala Jun 24 '22 at 07:54
  • You need to declare `relay` as a `Dictionary` and then the dictionary will have a key `4` – Charlieface Jun 24 '22 at 08:23
  • @Charlieface I've tried to do this but failed. Are you able to provide a quick example? – Mush Jun 24 '22 at 09:18
  • @YongShun I think it might but I couldn't get the example provided in that post to work. The dictionary had the keys of 1 and 2 but the values were "json_testing.Class3+Item" not the json objects? – Mush Jun 24 '22 at 09:19
  • @PeterCsala I've edited my question for clarification – Mush Jun 24 '22 at 09:22
  • @Mush Why do you need it as an array? Would `Dictionary` work as well for you? – Peter Csala Jun 24 '22 at 09:25
  • Please show the classes you have so far – Charlieface Jun 24 '22 at 09:53

1 Answers1

1

try this, you maybe need to add some validations, I don't know all details

   var relay =JObject.Parse(json)["io"]["relay"]
       .ToObject<Dictionary<string, JObject>>().First();

    var relayStatus = new int[6];
    var index = Convert.ToInt32(relay.Key);

    relayStatus[index] = (int) relay.Value["relayStatus"];

    var result = relayStatus[4];  // 1

but IMHO since you have only one relay status property, this code is better

var relayStatus = RelayStatus(json);

public int RelayStatus(string json)
{
    var relay = JObject.Parse(json)["io"]["relay"]
       .ToObject<Dictionary<string, JObject>>().First();

    return (int)relay.Value["relayStatus"];
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Thank you so much! The first piece of code that you provided does exactly what I was trying to achieve. What's more, you wrote in a way that a noob like me could understand what was going on. – Mush Jun 27 '22 at 03:22