1

I have JSON that looks like this (from the Philips HUE API):

{
    "1": {"name": "Bedroom"},
    "2": {"name": "Kitchen"}
}

When I try to deserialize this document I run into problems because the document is structured the way it is.

If it had been formated like this:

[
   {"nr": "1", "name": "Bedroom"},
   {"nr": "2", "name": "Kitchen"}
]

Everything would have been fine. Now I am forced to do string parsing in order to extract the data... :-(

Any ideas or suggestions?

1 Answer 1

2

I would deserialize to JObject and use it as Dictionary

var jObj = (JObject)JsonConvert.DeserializeObject(json);
Console.WriteLine(jObj["1"]["name"]);

or

dynamic jObj = JsonConvert.DeserializeObject(json);
Console.WriteLine(jObj["1"].name);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I will work with the dynamic approach and see what I can do with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.