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)
- Host: GitHub
- URL: https://github.com/syncfusionexamples/json-treeview-xamarin
- Owner: SyncfusionExamples
- Created: 2020-10-29T09:45:45.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-28T13:15:44.000Z (about 2 years ago)
- Last Synced: 2025-07-29T03:49:30.819Z (12 months ago)
- Topics: json, json-data, treeview, xamarin-forms
- Language: C#
- Homepage:
- Size: 1.21 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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**
