Parse JSON from an API with Continuous

In this lesson

  • Link Projects in Solution
  • Use WebClient to download JSON string
  • Use System.Xml.Linq to parse JSON

Register/Login to Get Source Code!

4:32 C# 200

Transcript

Tap on time to skip ahead

00:00

Intro

Continuous aims to enable developers to create Xamarin applications on their iPad, and by applications, we mean real world applications that might need to interface with a web API and download JSON and parse it into usable models or DTOs or what not. So in this video we’re gonna show how we can use Continuous to accomplish that.

00:33

Adding Project References in a Solution with Continuous

If we take a look here will say that I set up my solution, and one things I like to do is I like to separate out my models for my services from my view models from my actual views. And Continuous allows us to do this as well. So for example we will see I have this models project and inside it I have this very simple class right now called Brewery, and if you’ve watched some of my previous videos this will look somewhat familiar to you. And then in our best StlBrews.Services we’ll see I have a service class that will be responsible for downloading a JSON string and parsing it and then returning to whoever may be requesting a series of breweries and what not. Now something that’s interesting here is if I go into references you’ll see that I’ve added a reference to my StlBrews.Models project. And we can see that we can also add multiple other references to things like Xamarin.iOS or Xamarin.Forms.iOS. As well as some of our Continuous playground files like Pong or Map and what not.

01:37

Continuous Limitations

So if I go back. It will go into our BreweryService class. Now one of the limitations with Continuous running in a environment like iOS is right now we cannot download Nuget packages. So unfortunately we can’t just download the Newtonsoft JSON.net library and parse our JSON into models with just a couple lines of code. Nor can we use DataContracts there seems to be a limitation with some of the reflection and what not that DataContracts and some of the serialization stuff rely on. But instead we have some things that we can use to still accomplish this task. It just might require a couple more lines of code.

02:22

Using System.Net and WebClient with Continuous

If I move over to my watch tab. Will see that I have 2 methods here BreweryService. I have GetAll which will return a list of breweries but also have GetBreweryJson method here and I kind of stubbed it out separately from GetAll because I wanted to show that were able to use System.Net as well as Web Client. And if we go into our method here and let’s kind of just skip ahead from the GetAll stuff. You’ll see I can just say Client.DownloadString and in real time Continuous will actually run this code, interface with my server, and download this JSON file or JSON string I should say.

03:00

Parse JSON using Linq

So let’s get into the meat potatoes of our service. So if I take a look at GetAll here, you’ll see that I’m actually returning a list of breweries here and if I click on each of these breweries we’ll see that it is correctly parsing out the name. Like so. So how did I do this? Well one thing I had to do was create a memory stream. And here we can see I am getting the bytes of our GetBreweryJson method result. I then create a JSON reader from the JSONReaderWriterFactory. And from there I can start parsing the individual elements of our JSON payload. So you’ll see here I get a list of all the elements which represents each brewery element. And then I look through each brewery element and I add to our breweries list a new brewery. I then created this nice little helper method called GetJsonProp where I can pass it the current brewery element, the name of that particular field that I want, and then it can use Linq here down at the bottom to parse out that value.

04:12

Conclusion

So you can see that you can actually start creating real world applications with Continuous and you’re not limited from not being able to connect to a web API which is a pretty big requirement in this day and age of building applications.

Additional Info

Register to get access to additional resources and info.