Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

like image 525
dev_la Avatar asked Sep 01 '25 23:09

dev_la


1 Answers

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}

like image 199
Basil Kosovan Avatar answered Sep 03 '25 13:09

Basil Kosovan