简体   繁体   中英

Using Json.NET, how can I parse a list of objects with dynamic names into a List?

Below is an example of the JSON format I'm receiving from an external API:

"object": {
  "property_1": {
    "values": {
      "value": 1,
      "displayValue": "1"
    }
  },
  "property_5": {
    "values": {
      "value": 3,
      "displayValue": "3"
    }
  },
  "property_8": {
    "values": {
      "value": 1,
      "displayValue": "1"
    }
  },
  "property_14": {
    "values": {
      "value": 392,
      "displayValue": "392.0"
    }
  },
  "property_17": {
    "values": {
      "value": 14,
      "displayValue": "14"
    }
  }
}

There are somewhere around 100 different property types that can be sent, but they all follow the same structure. The only distinction is the name of the property ("property_1", "property_5", etc). Rather than attempting to write a class with a long list of properties that are not always used, I think it'd be much more useful to parse this into a list of objects, using the property name in the resulting class like so:

public class Property
{
    public string Name { get; set; }
    public PropertyValues Values { get; set; }
}

public class PropertyValues
{
    public double Value { get; set; }
    public string DisplayValue { get; set; }
}

In this case, the property name ("property_1", "property_5", etc) would be assigned to the Name field of the Property object.

How can this be accomplished using Json.NET?

var result  = JsonConvert.DeserializeObject<Root>(json);

You model is below


public class Root
{
    public Dictionary<string, ValueWrapper> Object { get; set; }
}
public class ValueWrapper
{
    public PropertyValues Values { get; set; }
}

public class PropertyValues
{
    public int Value { get; set; }
    public string DisplayValue { get; set; }
}

BTW: Json.Net can handle dynamic more easily than proposed by @MatíasFidemraizer...

var val = ((dynamic)JsonConvert.DeserializeObject(jsonstring))
          .@object.property_14.values.value;

var val = ((dynamic)JsonConvert.DeserializeObject(jsonstring))
          .@object["property_14"].values.value;

But this approach would require you to know property_14 will be returned by your server beforehand...

There's even another approach: you can use expando objects :

dynamic response = JsonConvert.DeserializeObject<ExpandoObject>("JSON TEXT", new ExpandoObjectConverter());

And you can access properties using dynamic typing:

long someValue = response.@object.property_1.values.value;

Also, since ExpandoObject implements IDictionary<string, object> , you can use it like a dictionary and check if a property exists:

if(((IDictionary<string, object>)response.@object).ContainsKey("property_1"))
{

}

It seems like you're going to save a lot of time going this way!

Answering hate on this answer

It seems like the most hate on my answer is focused on this @EZI comment:

And suppose you don't know property_14 before deserializing the json..

And the other answerer @LB argues this on his own answer:

But this approach would require you to know property_14 will be returned by your server beforehand...

In my case, I find that this concern is already addressed in my answer when I mention that ExpandoObject implements IDictionary<string, object> . Perhaps you don't know that property_14 will be part of deserialized object graph. No problem :

if(((IDictionary<string, object>)response.@object).ContainsKey("property_14"))
{
    // You've already addressed the issue, because you won't get a 
    // run-time exception since you'll access such property if it's already
    // in the returned response...
    object property_14 = response.@object.property_14;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM