5

I have a snippet of JSON that looks like this:

{"123":{"name":"test","info":"abc"}}

The 123 is an ID and can change on each request. This is beyond my control.

I want to Deserialize the JSON using JSON.NET. I have tried:

 User u = JsonConvert.DeserializeObject<User>(json);

However, this does not work unless I define the JsonProperty attribute like so:

[JsonProperty("123")]
public string ID { get; set; }

But of course I cannot actually do this because the ID 123 will change on every request.

How can I read the ID property using JSON.NET and apply it to the ID class?

4
  • 1
    It would work with a KeyValuePair<string, object>. You could try deserializing it in a temporary object buffer and then map it to your User instance.
    – Andrei V
    Jun 15, 2015 at 13:11
  • 2
    It's not malformed; just awkwardly formed. Basically it's a KVP of <int, User> where user has properties 'name' and 'info' Jun 15, 2015 at 13:15
  • I thought , In your class structure must be defined first class RootObject . In class Rootobject , u have to declare all json attribute as get set method. Jun 15, 2015 at 13:16
  • 1
    As pointed out, KeyValuePair<string, object> does not work. Using a Dictionary instead, as per @YuvalItzchakov's answer, will do the trick.
    – Andrei V
    Jun 15, 2015 at 13:25

3 Answers 3

5

Try this:

var json = "{\"123\":{\"name\":\"test\",\"info\":\"abc\"}}";

var rootObject = JsonConvert.DeserializeObject<Dictionary<string, User>>(json);
var user = rootObject.Select(kvp => new User 
                                    { ID = kvp.Key, 
                                      Name = kvp.Value.Name, 
                                      Info = kvp.Value.Info 
                                    }).First();

This does have some unnecessary overhead, but considering the circumstances, it would do.

2
  • 1
    Agreed but works - thank you.
    – Barry Kaye
    Jun 15, 2015 at 13:30
  • This solution is actually beautifull! Worked perfectly for me, even when the Value part is an array of objects!
    – awerdan
    Oct 21, 2020 at 2:53
2

I'd do it this way:

dynamic result = JsonConvert.DeserializeObject(json);

var myObject = result as JObject;
var properties = myObject.Properties();
var property = properties.FirstOrDefault(); // take first element 
string name = property.Name;

foreach (var item in properties)
{
    var jProperty = item as JProperty;
    var nestedJson = jProperty.Value.ToString();
    dynamic nestedResult = JsonConvert.DeserializeObject(nestedJson); // or put it into a model/data structure
}
2
  • Thanks - interesting but tricky to get at the actual user data now.
    – Barry Kaye
    Jun 15, 2015 at 13:29
  • 1
    i know... but you can deserialize the nested object also... updated my answer. Jun 15, 2015 at 13:39
0

How about having JSON formed as follows (if at all changing JSON schema is allowed):

{
    "ID": "123",
    "Properties":{"name":"test","info":"abc"}
}

This way it must be possible.

1
  • Sorry not allowed - I did bold it in the question ;-)
    – Barry Kaye
    Jun 15, 2015 at 13:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.