Consume php backend nested array on a C# Application – Json.NET

Lets assume you got a MYSQL database that contains data for your application and you want to consume this information on a C# Application in order to handle it on a client.

For the sake of this example the following table will be parsed

As depicted the following information is returned from the api:

  • the state of the response (succeeded or not)
  • a message that will show the response result
  • the actual result that in this case is a list of trips

Each trip contains id, fromlocation, tolocation, date and time properties that have to be returned to the application.

The php code that was used to create the data from the database is the following:

So in order to parse this json result we will need a plugin like Newtonsoft.Json

The class that will be used to encapsulate the response would be the following:

And then we will consume the json response with the following code in C#. In this example data from the API came from a POST request on a web Form.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.