Xamarin Forms: consuming data from a JSON file


Today I am going to explain a bit of mobile development and in particular how to create a method in Xamarin Forms, to consume data from a JSON file.

Xamarin Forms is a Microsoft framework designed to develop multi-platform applications.

The advantage of developing an application using Xamarin Forms is that you can then decide to deploy the same either to IOS or Android without additional efforts.

However, if you’re reading this article, it means you’re already acknowledged with the Xamarin Forms framework. So let’s go directly to the point: consuming a JSON file.

As you know the listview is one of the main components of a mobile application.

It can be used to display database records (reporting purpose) or to prompt the user for a selection among all the possible entries (data entry purpose).

One of the most efficient ways to build such a list is to provide all the entries using a web service. In the example that I am going to show you, I will assume that this web service has been already created and it has returned a JSON file. Let’s start.

The example

For clarification, the IDE I am using is Microsoft Visual Studio 2019.

My example is about a listview that will work as a selection of factory end-of-lines.

When I want to provide data from a source, I usually create a service class. In this case I created the EOLServices.cs file. I put it in a “Services” folder. This folder contains all the services classes that I need to be injected into a UI component.

Let’s dig into the code. The main method of this class is the one that returns a list of end-of-lines.

public List<EOL> GetEols()
 List<EOL> EOLs = new List<EOL>();
 string response = base.getStringResult();
 var results = 

foreach (ProductionDetail result in results)
     // JToken.ToObject is a helper method that uses JsonSerializer 
     var _eol = new EOL { 
                    WorkCenter = result.WorkCenter,
                    Code = result.EndOfLineCode, 
                    Description = result.EndOfLineName 
  return EOLs;

Be the first to comment

Leave a Reply

Your email address will not be published.