1

I have this JSON and I can't find a way to deserialize it. There is problem that MAC address in first "subnode" does not have a name. So, I don't know how to construct deserialized classes.

{
    'nodes': {
        '00:00:00:00:00:00:00:9D': {
            'node_id': 1,
            'rssi': 12,
            'temperature': 26,
            'pressure': 995,
            'humidity': 26,
            'gas_resistance': 465341,
            'iaq': 1
        }
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
Radim Pluskal
  • 101
  • 2
  • 9
  • If you're using JSON.NET you can use a [`JObject`](https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm). – Rain336 Nov 19 '19 at 09:32
  • Does this answer your question? [JSON .NET deserialize into complex type with object key/name](https://stackoverflow.com/questions/26141320/json-net-deserialize-into-complex-type-with-object-key-name) – xdtTransform Nov 19 '19 at 09:37
  • And `nodes` has an S but return a single object.. It may return an array when there is many object. In this case => https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n – xdtTransform Nov 19 '19 at 09:39
  • clearer dupe. https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net?rq=1 – xdtTransform Nov 19 '19 at 09:43

1 Answers1

2

You can use a Dictionary, for example:

public class Root
{
    public Dictionary<string, Node> Nodes { get; set; }
}

public class Node
{
    [JsonProperty("node_id")]
    public string NodeId { get; set; }

    // The other properties are left for you to fill out...
}

And deserialise like this:

var result = JsonConvert.DeserializeObject<Root>(json);
DavidG
  • 113,891
  • 12
  • 217
  • 223