0

I need to send a Array Of Object which is looks like below to Asp.Net API Contoller (Post Method and .Net Framework 4.5)

    var itemChanges = [];
   
   itemChanges.push({
        name: "Item1",
        desc: "percentage",
        value: 1
    });

    itemChanges.push({
        name: "Item2",
        desc: "detail",
        value: "sample_test"
    });

    itemChanges.push({
        name: "Item3",
        desc: "field",
        value: {
            name: "foo",                
            value: "test"
        }
    });

I send above array Object Json to API Contoller. But third item not parsed correctly. Adivce how can I receive it in Object Format. The 'value' property may have different types of Data. But it should be received

And I need to receive it in C# side.

For real time case check PayPal Update Subscription API Sample

Ananth Cool
  • 125
  • 8
  • Duplicate of https://stackoverflow.com/questions/20432166/how-to-deserialize-a-json-property-that-can-be-two-different-data-types-using-js – Caius Jard Sep 29 '21 at 18:51

1 Answers1

0

Use type object for property value, then you can type-check on the C# server-side using pattern matching (x is Type or x as Type) to handle specific Value types.

public class MyClass
{
    public string Name { get; set; }
    public string Desc { get; set; }
    public object Value { get; set; }
}
Andrew H
  • 875
  • 5
  • 20