In this lesson
- Add reference to Json.NET
- Parse Json into JArray
- Convert contents of JArray into Models
Tap on time to skip ahead
In a previous video where we explored Continuous (an IDE for .Net on iPad,) we used System.Link to parse Json that we downloaded from a web server into models. And this is pretty atypical from what a normal.Net developer would do. A regular .Net developer would use a library by Newtonsoft called Json.Net.
Adding Reference to Json.NET
Well, with the recent release of Continuous, we can use this library. So we’re gonna explore how we can use Json.Net. One of the first things that you need to do is you need to go into your project references and make sure you have Newtonsoft.Json selected as you can see here. And then at the top of your code file you need to import Newtonsoft.Json.Linq.
Parse Json into JArray
As you can see here I’ve updated our GetAll method to where we just say “JArray .Parse.” Then we “GetBreweryJson.” We then instantiate a list of breweries. And then we start parsing through the items in our JArray. And we use this syntax here where we cast a property of our item into the various model properties. And then at the end we return the collection of “breweries.”
Running the Code
If we go into the Watch tab we can see this code in action because Continuous is constantly re-compiling and running our code. So I’m going to select the brewery service. And I wanna make sure we’re getting Json from the server and we are. And then I’m going to tap the GetAll method. Then we will see that we have 16 items in our list. Now if I click on each of these will see the name of the particular brewery.
Updating Models and Parsing
With Continuous, it’s very easy to add new properties for our model and parse them. So I’m going to go back into our projects real quick. And I’m going to go into our model for Brewery. And I want to give it an additional property “Address.” And then I’m going to go back to our BreweryService. In our watch tab, if we click on a model will now see our address in addition to name. So with the latest version of Continuous we are able to use Json.Net to parse Json into models.