Google How to read local json file and display in Xamarin.Forms (C# - Xaml) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Saturday 9 March 2019

How to read local json file and display in Xamarin.Forms (C# - Xaml)

Introduction:
There are some situations when we want to parse and extract information from local json file which is inside of your project. You can also read one of my Json Parsing article which describes about parsing JSON response from RestAPI instead of local project file.

Requirements:
  • This article source code was prepared by using Visual Studio Professional for Mac (7.6.9). And it is better to install latest visual studio updates from here.
  • This sample project is Xamarin.Forms .NetStandardLibrary project and tested in Android emulator and iOS simulators.
  • Used Newtonsoft.Json Nuget Package version is V12.0.1.
Description:
To understand this article, we will take sample local json file(contacts.json) with below json string
  1. {  
  2.     "contacts": [{  
  3.             "id""c200",  
  4.             "name""Ravi Tamada",  
  5.             "email""ravi@gmail.com",  
  6.             "address""xx-xx-xxxx,x - street, x - country",  
  7.             "gender""male",  
  8.             "phone": {  
  9.                 "mobile""+91 0000000000",  
  10.                 "home""00 000000",  
  11.                 "office""00 000000"  
  12.             }  
  13.         },  
  14.         {  
  15.             "id""c201",  
  16.             "name""Johnny Depp",  
  17.             "email""johnny_depp@gmail.com",  
  18.             "address""xx-xx-xxxx,x - street, x - country",  
  19.             "gender""male",  
  20.             "phone": {  
  21.                 "mobile""+91 0000000000",  
  22.                 "home""00 000000",  
  23.                 "office""00 000000"  
  24.             }  
  25.         },  
  26.         {  
  27.             "id""c202",  
  28.             "name""Leonardo Dicaprio",  
  29.             "email""leonardo_dicaprio@gmail.com",  
  30.             "address""xx-xx-xxxx,x - street, x - country",  
  31.             "gender""male",  
  32.             "phone": {  
  33.                 "mobile""+91 0000000000",  
  34.                 "home""00 000000",  
  35.                 "office""00 000000"  
  36.             }  
  37.   
  38.         }  
  39.     ]  
  40. }  
And to parse above local json file, we need to follow below few steps
Step 1: Adding local file to your xamarin.forms project
Right click on your shared/library project => Add => New File => Web => Empty JSON File => Name it with "contacts.json" and paste above json string.
Note: Make sure to set Build Action to EmbeddedResource by right click on json file like below:


Step 2: Adding Newtonsoft.Json Nuget package
Right click on your shared/library project and Add => Add Nuget Packages => search for "Newtonsoft" and Add Package.
Step 3: Parsing Local Json File
Now we can parse our local json file with below code

  1. void GetJsonData()  
  2.         {  
  3.             string jsonFileName = "contacts.json";  
  4.             ContactList ObjContactList = new ContactList();  
  5.             var assembly = typeof(MainPage).GetTypeInfo().Assembly;  
  6.             Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");  
  7.             using (var reader = new System.IO.StreamReader(stream))  
  8.             {  
  9.                 var jsonString = reader.ReadToEnd();  
  10.   
  11.                 //Converting JSON Array Objects into generic list    
  12.                 ObjContactList = JsonConvert.DeserializeObject<ContactList>(jsonString);  
  13.             }  
  14.             //Binding listview with json string     
  15.             listviewConacts.ItemsSource = ObjContactList.contacts;  
  16.         }  
Demo Screens:
You can also directly work on below sample source code to understand this article.
LocalJsonSample
You can also see overview of this article from below youtube video. Also for more videos please don't forget to SUBSCRIBE our youtube channel from here.


FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.

Follow me always at @Subramanyam_B
Have a nice day by  :)

3 comments:

  1. Hello @Subramanyam

    I used this post and working well but i need some different.
    I want to get json item by item id its possible.

    ReplyDelete
  2. Hi, I'm in doubt on a project where it is more compressed to list json

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace MacVendas.Models.API
    {
    public class Pedido
    {
    public class Pedido2
    {
    public string id { get; set; }
    public string numero { get; set; }
    public string numero_ecommerce { get; set; }
    public string data_pedido { get; set; }
    public string data_prevista { get; set; }
    public string nome { get; set; }
    public string valor { get; set; }
    public string id_vendedor { get; set; }
    public string nome_vendedor { get; set; }
    public string situacao { get; set; }
    public string codigo_rastreamento { get; set; }
    public string url_rastreamento { get; set; }
    }

    public class Pedido1
    {
    public string pedido { get; set; }
    }

    public class Retorno
    {
    public string status_processamento { get; set; }
    public string status { get; set; }
    public string pagina { get; set; }
    public string numero_paginas { get; set; }
    public List pedidos { get; set; }
    }

    public class RootObject
    {
    public Retorno retorno { get; set; }
    }
    }
    }

    ReplyDelete
  3. Don't we need any permissions to access these local file?

    ReplyDelete

Search Engine Submission - AddMe