li x li x - 1 year ago 169
C# Question

Parsing JSON data from Restful API C#

So I have implemented a C# RESTful API for a recent side project and I have noticed that when making a GET request to the API I am provided with a lot of data when I only need around 8 or so entries, specifically the newest entries.

I have narrowed down this too one of two different issues, I believe my below fall back code to be somewhat primitive following a generic class structure that is created when deseralize object is called. The main problem could be because I am using a web client to download the JSON data then passing it over, could this be more sophisticated to allow the functionality I want?

using (WebClient wc = new WebClient())
var json = wc.DownloadString("//api call here");
jsonData = JsonConvert.DeserializeObject<List<T>>(json);

I am of course using NewtonSoft Json libary here.

Or secondly could it be that I need to make changes to the controller to allow a way for me to request only the 8 newest entries? I have little knowledge of changing API's other than allowing the VS magic to happen.

I mostly seek you're advice here because I could be worried about nothing but I just think my approach is wrong and if the data starts getting larger I should be improving on my existing knowledge.

Answer Source

To provide clarity on how this problem was solved it was mostly done through creating specific triggers on the existing API that allowed SQL to be run which is something that was I previously didn't know.

This allowed me to limit the amount of data that was returned through a generic sql command. A separate call was created on the API which returned all of the data and loaded the later objects into the dom using an async function.

Yes you can use SQL with api's generated by C# magic.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download