我正在尝试映射看起来像的 JSON
"ids": {
"id": {
"@value":"6763754764235874140"
}
}
我想将它映射到几个看起来像的类
class Property
{
public Ids Ids { get; set; }
}
class Ids
{
public string Id { get; set; }
}
所以基本上我想将 ids/id/@value
的值从 JSON 文档填充到类架构中的 Ids.Id
中。通过浏览文档,我认为我可以使用类似的东西
[JsonProperty(ItemConverterType=typeof(IdConverter))]
public string Id { get; set; }
并提供一个名为 IdConverter
的自定义 JsonConverter
子类。但是,当我这样做时,我的 IdConverter.ReadJson
永远不会被调用。我做错了什么?
最佳答案
看起来答案是 ItemConverterType
用于转换数组中的项目。使用 JsonProperty
和 JsonConverter
属性对属性进行双重注释有效:
[JsonProperty(PropertyName="id")]
[JsonConverter(typeof(IdConverter))]
public string Id { get; set; }
关于c# - 使用 Json.NET 的自定义映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11844374/