I want to add double quotation in list of each string item so that I can make a JSON request to send to api.
var response = default(HttpResponseMessage);
using var httpClient = CreateHttpClientForRequest();
var kpis = string.Join<string>(",", (IEnumerable<string>)keyProcessInstance);
using var requestMessage = new HttpRequestMessage(HttpMethod.Delete, PathfinderDeleteActiveUri)
{
Content = new StringContent ($@"[""{kpis}""]", Encoding.UTF8, "application/json")
};
I need this type of format in kpis variable.
["string1","string2","string"]
but not able to add "" for each string in the kpis.
2
Answers
You should use
System.Text.Json.JsonSerializer
to serialize your list to JSON.For educational purposes only, this is what you could do to make it work with your own approach.
You have to use backslash to use quotes in a string
"
.You don’t need to quote array values, the JSON serializer will do it automatically. In .NET 5 and later you can use JsonContent.Create to create a JsonContent object that serializes its payload automatically:
The default media type for JsonContent is
application/json
with CharSetutf-8