JSON Reader 2.0

Using the Visual Studio integrated development environment and the C# programming language, we will develop a cross-platform mobile application for downloading Chuck Norris jokes in JSONarrow-up-right format.

circle-info

Information

JSON (JavaScript Object Notation) is a text-based open standard designed for human-readable data interchange. It originates from the JavaScript scripting language to represent simple data structures and associative arrays called objects.

Start

  1. Launch the Visual Studio integrated development environment.

  2. Create a new project: Visual C# > Cross-Platform > Mobile App (Xamarin.Forms).

  3. Name the project: JSON Reader 2.0.

Add additional packages to the project by installing Newtonsoft.Json from: Tools > NuGet Package Manager > Package Manager Console, by running the following command in the console:

PM> Install-Package Newtonsoft.Json -Version 13.0.1

Root.cs

Add a new class Root.cs that will be used to deserialize the data returned by the service.

public class Root
{
    public List<object> categories { get; set; }

    public string created_at { get; set; }

    public string icon_url { get; set; }

    public string id { get; set; }

    public string updated_at { get; set; }

    public string url { get; set; }

    public string value { get; set; }
}

Note

  1. Load and copy an example JSON from: https://api.chucknorris.io/jokes/random

  2. Generate the C# class for the selected JSON from: http://json2csharp.com/

MainPage.xaml

The MainPage.xaml file contains the source code for the application's user interface design and is written in XAML. Copy (Ctrl+C) and paste (Ctrl+V) the fragment below into your application.

MainPage.xaml.cs

The MainPage.xaml.cs file contains the source code for the application's business logic and is written in C#. Copy (Ctrl+C) and paste (Ctrl+V) the fragment below into your application.

Demo

Run the application from the menu: Debug > Start Debugging or by pressing the F5 key.

Fig. 2.63. Demonstration of the cross-platform mobile application for downloading Chuck Norris jokes

Last updated