使用动态 属性 名称反序列化 json
Deserializing json with dynamic property name
这里有人提出了类似的问题 。我无法让它在我的情况下工作,如下所示:
我有以下JSON
{
"notes": {
"data": [{
"book": {
"items": [{
"page": "page 1",
"comment": "Some notes"
},
{
"page": "page 1",
"comment": "Some notes"
}
]
}
},
{
"journal": {
"items": [{
"page": "page 1",
"comment": "Some notes"
},
{
"page": "page 1",
"comment": "Some notes"
}
]
}
},
{
"magazine": {
"items": [{
"page": "page 1",
"comment": "Some notes"
},
{
"page": "page 1",
"comment": "Some notes"
}
]
}
}
]
}
}
为 JSON 序列化创建了以下 类:
public class TestParse
{
public Note Notes { get; set; }
}
public class Note
{
public IList<Data> Data { get; set; }
}
public class Data
{
public Source Source { get; set; }
}
[JsonConverter(typeof(MyConverter))]
public class Source
{
public IList<Items> Items { get; set; }
}
public class Items
{
public string Page { get; set; }
public string Comment { get; set; }
}
public class MyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Source);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
var jo = JObject.Load(reader);
var req = new Data
{
Source = jo.ToObject<Source>()
};
return req;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var req = (Source)value;
var jo = new JObject(
new JObject(req.Items));
jo.WriteTo(writer);
}
}
}
我仍然无法反序列化"Source."任何指针都值得赞赏。
在您引用的 中,动态键由同一对象中另一个 属性 的值确定。因此,需要转换器。
在你的情况下,你有动态密钥,但它们不依赖于任何东西。
您在这里真的不需要转换器;您可以只使用字典来处理动态键。
在 Note
class 中更改此行:
public IList<Data> Data { get; set; }
对此:
public IList<IDictionary<string, Source>> Data { get; set; }
并从您的 Source
class 中删除 [JsonConverter]
属性。然后它应该工作。
(您也可以删除 Data
class,因为它不需要。)
Fiddle: https://dotnetfiddle.net/80lIRI
这里有人提出了类似的问题
我有以下JSON
{
"notes": {
"data": [{
"book": {
"items": [{
"page": "page 1",
"comment": "Some notes"
},
{
"page": "page 1",
"comment": "Some notes"
}
]
}
},
{
"journal": {
"items": [{
"page": "page 1",
"comment": "Some notes"
},
{
"page": "page 1",
"comment": "Some notes"
}
]
}
},
{
"magazine": {
"items": [{
"page": "page 1",
"comment": "Some notes"
},
{
"page": "page 1",
"comment": "Some notes"
}
]
}
}
]
}
}
为 JSON 序列化创建了以下 类:
public class TestParse
{
public Note Notes { get; set; }
}
public class Note
{
public IList<Data> Data { get; set; }
}
public class Data
{
public Source Source { get; set; }
}
[JsonConverter(typeof(MyConverter))]
public class Source
{
public IList<Items> Items { get; set; }
}
public class Items
{
public string Page { get; set; }
public string Comment { get; set; }
}
public class MyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Source);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
var jo = JObject.Load(reader);
var req = new Data
{
Source = jo.ToObject<Source>()
};
return req;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var req = (Source)value;
var jo = new JObject(
new JObject(req.Items));
jo.WriteTo(writer);
}
}
}
我仍然无法反序列化"Source."任何指针都值得赞赏。
在您引用的
在你的情况下,你有动态密钥,但它们不依赖于任何东西。 您在这里真的不需要转换器;您可以只使用字典来处理动态键。
在 Note
class 中更改此行:
public IList<Data> Data { get; set; }
对此:
public IList<IDictionary<string, Source>> Data { get; set; }
并从您的 Source
class 中删除 [JsonConverter]
属性。然后它应该工作。
(您也可以删除 Data
class,因为它不需要。)
Fiddle: https://dotnetfiddle.net/80lIRI