0

I have some JSON that i am sending over to my C# API and it looks like the following

 {
  "currency": "BTC",
  "amount": "0.00049659",
  "type": "bankToExchange"
 }

The issue is when the model arrives in my controller, the type property is changed to @type which is making the post request fail.

The API I am trying to connect to uses type, so this cannot be changed. The post works in Postman, so is there a work around for this?

AndreaT
  • 367
  • 2
  • 10
Justin
  • 65
  • 1
  • 6

2 Answers2

0

Add the DataMember name property on your type using JsonProperty:

[DataMember(Name = "@type")] //if not using NewtonSoft
[JsonProperty("@type")] //if using NewtonSoft
public string type { get; set; }

enter image description here

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

Use Data member attribute with property name. You can use by creating class for your json, as follows

    [DataContract]
    public class Sample{
         [DataMember(Name = "@type")]
         public string Type{get;set;}
    }

You can try with another approach as well, which is elegant and more meaning full if you add comment for appending @ before property name :

     public class Sample{
             public string @type{get;set;}
        }

For reference: object to deserialize has a C# keyword

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • so im sending it to a 3rd party api, so this doesnt seem to be working – Justin Apr 07 '18 at 22:58
  • which option is not working, the second one? Have you tried with first one. @Sadique already suggested if you are using Newton.Json for deserializing, then you can use JsonProperty. If 3rd party API is public, then please let me know endpoints, I can try by my own. – Prasad Telkikar Apr 08 '18 at 11:07