How to bind dynamic property?

Pardeep Sharma

I am parsing some Json data that Contains Json objects as

  • "0": "some data" ,
  • "1" : "other data".

After converting it to c# classes , code converted as :

public class Data
{
    public List<Items> photos { get; set; }
}

public class Items
{
    public string __invalid_name__3 { get; set; }
    public string __invalid_name__0 { get; set; }
    public string __invalid_name__2 { get; set; }
} 

This data won't deserialize properly . I have tried both Jsoncovert as well as DataContract deserializer.

So I took

List<Items> as List<dynamic>. 

But the problem is keys name are still as "0" , "1" and this won't be able to bind to my xaml UI end.

Is there any way to bind this dynamic data to UI?

tyler

Using the DataContractJsonSerializer you can set the Name property on your DataMemberAttributes to serialize the object with different names than the names of your properties in C#:

    [DataContract]
    public class Data
    {
        [DataMember]
        public List<Items> photos { get; set; }
    }

    [DataContract]
    public class Items
    {
        [DataMember(Name = "3")]
        public string Three { get; set; }
        [DataMember(Name="0")]
        public string Zero { get; set; }
        [DataMember(Name = "2")]
        public string Two { get; set; }
    }

    public static void TestJson()
    {
        var json = "{\"photos\":[{\"0\":\"some data\",\"2\":\"other data\",\"3\":\"another data\"}]}";

        var serializer = new DataContractJsonSerializer(typeof(Data));
        Data data = serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json))) as Data;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind IsEnabled property to a more dynamic property in ViewModel?

From Dev

How to bind dynamic radio buttons to controller property in Ember

From Dev

Dynamic Radio button bind checked property is dynamic

From Dev

How to bind textblock with property

From Dev

How to Bind Attached Property

From Dev

How to Bind the Property of an Instance?

From Dev

How to bind a ContentView to a property?

From Dev

How to bind a date for a dynamic query

From Dev

How to bind to a property in view but not in viewmodel?

From Dev

How to bind property using a formatter

From Dev

How to bind a textbox to class Property

From Dev

How to bind control to object property?

From Dev

How to bind property of a ObservableCollection in XAML

From Dev

How to bind a Promise to a component property?

From Dev

How to bind to attached property in UWP?

From Dev

How to bind control to object property?

From Dev

How to bind ComboBox to ObservableCollection property?

From Dev

How to bind checkBox to a boolean property?

From Dev

How to bind user control property to other property?

From Dev

How to get dynamic type property?

From Dev

How to bind dynamic data in one bootstrap modal

From Dev

How to bind bootstrap tooltip on dynamic elements

From Dev

How to bind a dynamic class to an element in Handlebars and Ember?

From Dev

How to bind a dynamic value inside a visualstate

From Dev

How to bind dynamic content to angular-toastr?

From Dev

How can I bind to the Center property of an EllipseGeometry?

From Dev

How to bind collection dependency property in UserControl

From Dev

How to bind properly to a service property in AngularJS?

From Dev

How to Bind to Another Control Property in WPF

Related Related

HotTag

Archive