我连接到一个第三方 Web 服务,返回一个复杂的 JSON 对象,但里面只包含了我实际需要的一些信息。
基本上,我只需要 "value" 数组。从这个数组中,我只需要 "Id"、"Title" 和 "Status" 属性。
我想把这些属性放入一个名为 Project 的 C# 类中。这是我的类:
public class Project
{
public String Id { get; set; }
public String Title { get; set; }
public String Status { get; set; }
}
我正在尝试使用这段代码读取JSON并进行转换:
using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var serializer = new JsonSerializer();
var jsonTextReader = new JsonTextReader(reader);
returnValue = serializer.Deserialize<Project>(jsonTextReader);
}
}
示例JSON:
{
"odata.metadata":"http://school.edu/Api/1/$metadata#Projects",
"odata.count":"3",
"value":[
{
"odata.id":"http://school.edu/Api/1/Projects('123')",
"RelatedProjects@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/RelatedProjects",
"Tags@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/Tags",
"TimedEvents@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/Categories",
"ep@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/ep",
"#CreateLike":{
"target":"http://school.edu/Api/1/Projects('123')/CreateLike"
},
"#CreateShortcut":{
"target":"http://school.edu/Api/1/Projects('123')/CreateShortcut"
},
"#Play":{
"target":"http://school.edu/Play/123"
},
"#SendInvitation":{
"target":"http://school.edu/Api/1/Projects('123')/SendInvitation"
},
"#CopyProject":{
"target":"http://school.edu/Api/1/Projects('123')/CopyProject"
},
"#AddVideoPodcast":{
"target":"http://school.edu/Api/1/Projects('123')/AddVideoPodcast"
},
"#AddEP":{
"target":"http://school.edu/Api/1/Projects('123')/AddEP"
},
"Id":"123",
"Title":"Test Title 1",
"Status":"Viewable"
},
{
"odata.id":"http://school.edu/Api/1/Projects('456')",
"RelatedProjects@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/RelatedProjects",
"Tags@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/Tags",
"TimedEvents@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/Categories",
"ep@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/ep",
"#CreateLike":{
"target":"http://school.edu/Api/1/Projects('456')/CreateLike"
},
"#CreateShortcut":{
"target":"http://school.edu/Api/1/Projects('456')/CreateShortcut"
},
"#Play":{
"target":"http://school.edu/Play/456"
},
"#SendInvitation":{
"target":"http://school.edu/Api/1/Projects('456')/SendInvitation"
},
"#CopyProject":{
"target":"http://school.edu/Api/1/Projects('456')/CopyProject"
},
"#AddVideoPodcast":{
"target":"http://school.edu/Api/1/Projects('456')/AddVideoPodcast"
},
"#AddEP":{
"target":"http://school.edu/Api/1/Projects('456')/AddEP"
},
"Id":"456",
"Title":"Test Title 2",
"Status":"Viewable"
},
{
"odata.id":"http://school.edu/Api/1/Projects('789')",
"RelatedProjects@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/RelatedProjects",
"Tags@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/Tags",
"TimedEvents@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/Categories",
"ep@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/ep",
"#CreateLike":{
"target":"http://school.edu/Api/1/Projects('789')/CreateLike"
},
"#CreateShortcut":{
"target":"http://school.edu/Api/1/Projects('789')/CreateShortcut"
},
"#Play":{
"target":"http://school.edu/Play/789"
},
"#SendInvitation":{
"target":"http://school.edu/Api/1/Projects('789')/SendInvitation"
},
"#CopyProject":{
"target":"http://school.edu/Api/1/Projects('789')/CopyProject"
},
"#AddVideoPodcast":{
"target":"http://school.edu/Api/1/Projects('789')/AddVideoPodcast"
},
"#AddEP":{
"target":"http://school.edu/Api/1/Projects('789')/AddEP"
},
"Id":"789",
"Title":"Test Title 3",
"Status":"Viewable"
}
],
"odata.nextLink":"http://school.edu/Api/1/Folders('xyz')/Projects?$skip=10&$top=10"
}
我只是收到一个null对象而已,但在调试器中,我可以看到它从Web服务获取了所有的JSON数据。
我该如何从JSON中获取所需内容,构建我的C#对象,忽略其余部分呢?