[英]NewtonSoft Json deserialization property list as array of objects
I have an odd Json result set that has a repeating (but changeable) property that I need to convert to an array of an object type, eg 我有一个奇怪的Json结果集,它具有重复(但可更改)的属性,我需要将其转换为对象类型的数组,例如
"result": {
"documents": {
"abcd": {
"propertyX": 0
"propertyY": "A"
},
"efgh": {
"propertyX": 5
"propertyY": "B"
},
"ijkl": {
"propertyX": 2
"propertyY": "C"
}
}
}
What I'd like to do is to have my Result object with a document property, and this have an array of "items". 我想要做的是让我的Result对象具有document属性,并且它有一个“items”数组。 Each item object will contain "propertyX", "propertyY" etc. Unfortunately "abcd", "efgh" etc. are a random list of items but they are rendered as distinct properties.
每个项目对象将包含“propertyX”,“propertyY”等。不幸的是,“abcd”,“efgh”等是项目的随机列表,但它们被呈现为不同的属性。
Is there a straightforward way of handling this or would I need a custom converter? 有没有直接的方法来处理这个或我需要一个自定义转换器?
Yes, the straightforward way to handle this is to use a Dictionary<string, Item>
for your documents
property. 是的,处理此问题的直接方法是使用
Dictionary<string, Item>
作为documents
属性。 The random document names would become the keys of the dictionary. 随机文档名称将成为字典的键。 You can declare the classes like this:
您可以声明这样的类:
class RootObject
{
public Result Result { get; set; }
}
class Result
{
public Dictionary<string, Item> Documents { get; set; }
}
class Item
{
public string PropertyX { get; set; }
public string PropertyY { get; set; }
}
Then deserialize the JSON like this: 然后像这样反序列化JSON:
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
Fiddle: https://dotnetfiddle.net/lTDGj3 小提琴: https : //dotnetfiddle.net/lTDGj3
If you do not want a dictionary in your class and instead would really rather have an array (or list) of items, then yes, you would need a converter. 如果你不想在你的班级中使用字典而不是想要一个项目的数组(或列表),那么是的,你需要一个转换器。 In that case, you would declare your classes like this:
在这种情况下,您将声明您的类如下:
class RootObject
{
public Result Result { get; set; }
}
class Result
{
[JsonConverter(typeof(DocumentListConverter))]
public List<Item> Documents { get; set; }
}
class Item
{
public string Name { get; set; }
public string PropertyX { get; set; }
public string PropertyY { get; set; }
}
The custom converter class for the document list would look something like this: 文档列表的自定义转换器类看起来像这样:
class DocumentListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<Item>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
List<Item> items = new List<Item>();
foreach (JProperty prop in jo.Properties())
{
Item item = prop.Value.ToObject<Item>();
item.Name = prop.Name;
items.Add(item);
}
return items;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And you would deserialize in the same way as before: 你会像以前一样反序列化:
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
Fiddle: https://dotnetfiddle.net/xWRMGP 小提琴: https : //dotnetfiddle.net/xWRMGP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.