An open API service indexing awesome lists of open source software.

https://github.com/syncfusionexamples/json-treeview-xamarin

This repository contains the sample about how to bind JSON data to Xamarin.Forms TreeView (SfTreeView)
https://github.com/syncfusionexamples/json-treeview-xamarin

json json-data treeview xamarin-forms

Last synced: about 2 months ago
JSON representation

This repository contains the sample about how to bind JSON data to Xamarin.Forms TreeView (SfTreeView)

Awesome Lists containing this project

README

          

# How to bind JSON data to Xamarin.Forms TreeView (SfTreeView)

You can bind the data from [JSON](http://www.json.org/) (JavaScript Object Notation) file to the Xamarin forms [SfTreeView](https://help.syncfusion.com/xamarin/treeview/getting-started?) using [ItemSource](https://help.syncfusion.com/cr/xamarin/Syncfusion.XForms.TreeView.SfTreeView.html?_ga=2.169034657.1409646585.1603702470-522025424.1599212225#Syncfusion_XForms_TreeView_SfTreeView_ItemsSource) property.

You can also refer our article from Syncfusion KnowledgeBase

https://www.syncfusion.com/kb/12002/how-to-bind-json-data-to-xamarin-forms-treeview-sftreeview

**STEP 1:** Create a JSON data model

``` c#
public class Cities
{
public string Name { get; set; }
}

public class States
{
public List Cities { get; set; }
public string Name { get; set; }
}

public class Countries
{
public List States { get; set; }
public string Name { get; set; }
}
``` c#
**STEP 2:** Accessed the JSON file from local folder and use [StreamReader](https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader?view=netcore-3.1) to reads the data and return as a ObservableCollection property.

``` c#
private void GenerateSource()
{
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("TreeViewXamarin.Data.navigation.json");
using (StreamReader sr = new StreamReader(stream))
{
var jsonText = sr.ReadToEnd();
ImageNodeInfo = new ObservableCollection();
var MyArrayData = JsonConvert.DeserializeObject>(jsonText);
foreach (var data in MyArrayData)
{
ImageNodeInfo.Add(data);
}
}
}
```
**STEP 3:** Bind the JSON collection data to the SfTreeView ItemSource.
``` xml













```
**Output**
![JSONTreeViewXamarin](https://github.com/SyncfusionExamples/json-treeview-xamarin/blob/main/ScreenShots/JSONTreeViewXamarin.gif)