Selectively read a piece of JSON data using a JsonSerializer and populate a C # object

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?

+5
source share
1 answer

If you can use Json.NET (Newtonsoft json), you can use Linq-to-Json like this [1]

 //using Newtonsoft.Json.Linq; var jsonString = File.ReadAllText(@"C:YourDirectory\file.json"); //source var projects = new List<Project>(); //Your result JObject data = JObject.Parse(jsonString); foreach (var value in data["value"]) { projects.Add(new Project { Id = value["Id"].ToString(), Status = value["Status"].ToString(), Title = value["Title"].ToString() }); } 

Or you can also deserialize JObject as follows: [2]

 var jsonReader = data["value"].CreateReader(); projects = new JsonSerializer().Deserialize<List<Project>>(jsonReader); 

Both work great, but which one is better?

The second approach means less code (especially if you have many properties in the Project class, you have to write many lines of code to map each property in the code [1]).

But the performance of the first approach is many times better! For json data, code [1] runs in about 1 ms , while code [2] takes more than 100 ms !


Update

After introducing James Newton King (who wrote Json.NET :), there is another more elegant way to do this [3]

 projects = data["value"].ToObject<List<Project>>(); 

And guess what! This code [3] takes almost half the time of the approach [1]. Therefore, from all points of view, this should be the best approach!

+5
source

Source: https://habr.com/ru/post/1232483/


All Articles