我试图扩展这里给出的JSON.net示例 http://james.newtonking.com/projects/json/help/CustomCreationConverter.html
我有另一个派生自基类/接口的子类
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class Artist : Person
{
public string Skill { get; set; }
}
List<Person> people = new List<Person>
{
new Employee(),
new Employee(),
new Artist(),
};
Run Code Online (Sandbox Code Playgroud)
如何将Json反序列化回List <Person>
[
{
"Department": "Department1",
"JobTitle": "JobTitle1",
"FirstName": "FirstName1", …Run Code Online (Sandbox Code Playgroud) 我正在尝试修复我的SendGridPlus库来处理SendGrid事件,但是我在API中对类别的处理方式不一致时遇到了一些麻烦.
在以下示例从SendGrid API引用获取的有效内容中,您会注意到category每个项的属性可以是单个字符串,也可以是字符串数组.
[
{
"email": "john.doe@sendgrid.com",
"timestamp": 1337966815,
"category": [
"newuser",
"transactional"
],
"event": "open"
},
{
"email": "jane.doe@sendgrid.com",
"timestamp": 1337966815,
"category": "olduser",
"event": "open"
}
]
Run Code Online (Sandbox Code Playgroud)
看起来像我这样制作JSON.NET的选择是在字符串进入之前修复字符串,或者配置JSON.NET以接受不正确的数据.如果我能逃脱它,我宁愿不做任何字符串解析.
有没有其他方法我可以使用Json.Net处理这个?
此Imgur api调用返回一个列表,其中包含json中表示的Gallery Image和Gallery Album类.
我不知道如何使用Json.NET自动反序列化这些,因为没有$ type属性告诉反序列化器要表示哪个类.有一个名为"IsAlbum"的属性可用于区分两者.
这个问题似乎显示了一种方法,但它看起来有点像黑客.
我该如何反序列化这些类?(使用C#,Json.NET).
样本数据:
图库图片
{
"id": "OUHDm",
"title": "My most recent drawing. Spent over 100 hours.",
...
"is_album": false
}
Run Code Online (Sandbox Code Playgroud)
画廊专辑
{
"id": "lDRB2",
"title": "Imgur Office",
...
"is_album": true,
"images_count": 3,
"images": [
{
"id": "24nLu",
...
"link": "http://i.imgur.com/24nLu.jpg"
},
{
"id": "Ziz25",
...
"link": "http://i.imgur.com/Ziz25.jpg"
},
{
"id": "9tzW6",
...
"link": "http://i.imgur.com/9tzW6.jpg"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
}