We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind JSON data to Xamarin.Forms DataForm (SfDataForm) ?

Platform: Xamarin.Forms |
Control: SfDataForm |
Published Date: April 1, 2020 |
Last Revised Date: April 1, 2020

You can bind the data from JSON (JavaScript Object Notation) in Xamarin.Forms

SfDataForm.

C#

Create a JSON data model.

/// <summary>   
/// Represents custom data properties.   
/// </summary> 
public class JSONData
{
       public string UserName { get; set; }
       public string UserGender { get; set; }
       public string UserMail { get; set; }
       public string UserCountry { get; set; }
}

 

Note:

Convert String type into DateTime type when adding items to the local collection from JSON data model, since JSON does not support DateTime type.

C#

Create data for the data model.

//// Add data for JSON data model
private string JsonData =
          "[{\"UserName\": \"Chan\",\"UserGender\": \"Male\",\"UserMail\": \"chan@yyy.com\",\"UserCountry\": \"Japan\", \"UserBirthDate\": \"05/01/1996\"}]";
 

C#

Deserialize the JSON data as list of JSON data model.

List<JSONData> jsonDataCollection = JsonConvert.DeserializeObject<List<JSONData>>(JsonData);

C#

Load the JSON data list into the DataForm model.

foreach (var data in jsonDataCollection)
{
                this.ContactsInfo.Name = data.UserName;
                this.ContactsInfo.Gender = data.UserGender;
                this.ContactsInfo.Email = data.UserMail;
                this.ContactsInfo.Country = data.UserCountry;
                this.ContactsInfo.DateOfBirth = Convert.ToDateTime(data.UserBirthDate);
}

Output

A screenshot of a cell phone

Description automatically generated

View Sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon