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:
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
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.
- {
- "contacts": [{
- "id": "c200",
- "name": "Ravi Tamada",
- "email": "ravi@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender": "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c201",
- "name": "Johnny Depp",
- "email": "johnny_depp@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender": "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c202",
- "name": "Leonardo Dicaprio",
- "email": "leonardo_dicaprio@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender": "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- }
- ]
- }
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
- void GetJsonData()
- {
- string jsonFileName = "contacts.json";
- ContactList ObjContactList = new ContactList();
- var assembly = typeof(MainPage).GetTypeInfo().Assembly;
- Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");
- using (var reader = new System.IO.StreamReader(stream))
- {
- var jsonString = reader.ReadToEnd();
- //Converting JSON Array Objects into generic list
- ObjContactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
- }
- //Binding listview with json string
- listviewConacts.ItemsSource = ObjContactList.contacts;
- }
You can also directly work on below sample source code to understand this article.
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.
Hello @Subramanyam
ReplyDeleteI used this post and working well but i need some different.
I want to get json item by item id its possible.
Hi, I'm in doubt on a project where it is more compressed to list json
ReplyDeleteusing 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; }
}
}
}
Don't we need any permissions to access these local file?
ReplyDelete