简体   繁体   中英

Sending C# Reserved Word as Property of PostData Api

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?

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; }

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM