|
1 |
| -# json-binding-dataform-xamarin |
2 |
| -How to bind JSON data to Xamarin.Forms DataForm (SfDataForm) ? |
| 1 | +# How to bind JSON data to Xamarin.Forms DataForm (SfDataForm) ? |
| 2 | + |
| 3 | +You can bind the data from JSON (JavaScript Object Notation) in Xamarin.Forms SfDataForm. |
| 4 | + |
| 5 | +You can refer the following article. |
| 6 | + |
| 7 | +https://www.syncfusion.com/kb/11310/how-to-bind-json-data-to-xamarin-forms-dataform-sfdataform |
| 8 | + |
| 9 | +Create a JSON data model. |
| 10 | +``` c# |
| 11 | +/// <summary> |
| 12 | +/// Represents custom data properties. |
| 13 | +/// </summary> |
| 14 | +public class JSONData |
| 15 | +{ |
| 16 | + public string UserName { get; set; } |
| 17 | + public string UserGender { get; set; } |
| 18 | + public string UserMail { get; set; } |
| 19 | + public string UserCountry { get; set; } |
| 20 | +} |
| 21 | +``` |
| 22 | +**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. |
| 23 | + |
| 24 | +Create data for the data model. |
| 25 | +``` c# |
| 26 | +//// Add data for JSON data model |
| 27 | +private string JsonData = |
| 28 | + "[{\"UserName\": \"Chan\",\"UserGender\": \"Male\",\"UserMail\": \"chan@yyy.com\",\"UserCountry\": \"Japan\", \"UserBirthDate\": \"05/01/1996\"}]"; |
| 29 | +``` |
| 30 | +Deserialize the JSON data as list of JSON data model. |
| 31 | + |
| 32 | +``` c# |
| 33 | +List<JSONData> jsonDataCollection = JsonConvert.DeserializeObject<List<JSONData>>(JsonData); |
| 34 | +``` |
| 35 | + |
| 36 | +Load the JSON data list into the DataForm model. |
| 37 | + |
| 38 | +``` c# |
| 39 | +foreach (var data in jsonDataCollection) |
| 40 | +{ |
| 41 | + this.ContactsInfo.Name = data.UserName; |
| 42 | + this.ContactsInfo.Gender = data.UserGender; |
| 43 | + this.ContactsInfo.Email = data.UserMail; |
| 44 | + this.ContactsInfo.Country = data.UserCountry; |
| 45 | + this.ContactsInfo.DateOfBirth = Convert.ToDateTime(data.UserBirthDate); |
| 46 | +} |
| 47 | +``` |
| 48 | +**Output** |
| 49 | + |
| 50 | + |
0 commit comments