I am trying to create a JSON string content by passing a double typed value (named time
). My JSON content is also containing a root header object (named UserAddTime
). I would like to add time
in Minutes
object.
double time = 60;
var content = new StringContent
(@"{
'UserAddTime':
{
'Minutes':"+time+",
'TotalPrice':0.00,
'IsTaxIncluded':false
}
}",
Encoding.UTF8, "application/json");
It doesn’t allow me to add time like this:
'Minutes':"+time+",
And shows me the red line error content LOC:
I am trying this solution, but not able to get my expected results like:
{
"UserAddTime" : {
"Minutes" : 60,
"TotalPrice" : 0.00,
"IsTaxIncluded" : false
}
}
Your any help will be really appreciated!
1
Instead of constructing the JSON manually, you should look for constructing an object and then serialize via JSON library such as System.Text.Json or Newtonsoft.Json.
using System.Text.Json;
double time = 60;
var body = new
{
UserAddTime = new
{
Minutes = time,
TotalPrice = 0.00,
IsTaxIncluded = false
}
};
var content = new StringContent(JsonSerializer.Serialize(body),
Encoding.UTF8, "application/json");