I connect to a third-party web service that returns a complex JSON object that contains only a few bits of information that I really need.
Basically, I just need an array in the "value". From this array, I just need the properties "Id", "Title" and "Status".
I want to put these attributes in a C # class called Project. This is my class:
public class Project { public String Id { get; set; } public String Title { get; set; } public String Status { get; set; } }
I am trying to use this code to read JSON and do the conversion:
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 example:
{ "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" }
I just return the null object. But in the debugger, I see that it retrieves all the JSON data from the web service.
How can I get what I need from JSON, build my C # objects and ignore everyone else?