简体   繁体   中英

Post json Object which property name has space to third party api from c#

I'm calling third party api from my asp.net mvc project. That api requires json object that has property name with space. But in c# we can't create property name with space. How Can I do that, I'm stuck?

I have tried using JsonProperty, but It is not working. I have tried to replace string in serialize string and then send that string to api but that gives me total error.

{
 "Single":14000,
 "Double":14500,
 "Triple":15000,
 "ExtraBed":15500,
 "ExtraChild":16000,
 "ExtraAdult":16000
}

But instead of ExtraBed, I have to pass as 'Extra Bed'.

JsonPropertyAttribute doesn't impact on JavaScriptSerializer . There is no attribute for JavaScriptSerializer in order to change property name.You can write a custom JavaScriptConverter for it, but I recomend just use Newtonsoft .

 class AxisRoom
 {
     [JsonProperty("Extra Bed")]
     public decimal ExtraBed { get; set; }
 }


 AxisRoom _AxisRoom = new AxisRoom { ExtraBed = 3 };
 var result = JsonConvert.SerializeObject(_AxisRoom);

result is equal to {"Extra Bed":3.0}

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