0

Currently in my code I am appending my parameter "users" this way:

request.AddParameter("application/json", "{\"users\": " + users+ "}", ParameterType.RequestBody);

I have seen other examples in which the parameter can be appended using curly braces, something like this:

request.AddParameter("application/json", "{\"users\": " {users} "}", ParameterType.RequestBody);

But that is not the correct syntax, would it be possible someone show me how to append the panelists parameter correctly, using curly braces rather that (+) sign?

Thank you.

erasmo carlos
  • 664
  • 5
  • 16
  • 37
  • 4
    rule #1 about json: **do not write it yourself.** serialise an object instead. there's enough libraries that do it, and they do it better than selfmade code. – Franz Gleichmann Jul 07 '20 at 17:45
  • I guess what you are looking for is $"{{\"users\": {users}}}" – ddfra Jul 07 '20 at 17:47
  • What is `users`? Is this also a string containing a serialized json array? If it is not then you should use a serialization library like [json.net](https://www.newtonsoft.com/json) – Igor Jul 07 '20 at 17:47
  • 1
    The previous comments are correct, you should not hand craft JSON; but they do not answer your question. What you are referring to is [`string interpolation`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) – ColinM Jul 07 '20 at 17:52
  • 1
    Duplicate shows what you trying to achieve ("string interpolation") but as everyone said in the comments you should not build JSON by hand - it is far easier and safer to use proper serialization i.e. using JSON.Net – Alexei Levenkov Jul 07 '20 at 18:26

1 Answers1

-1

What you are trying to achieve is called string interpolation.

In your case the correct statement should be:

request.AddParameter("application/json", $"{{\"users\": \"{users}\"}}", ParameterType.RequestBody);

Note that the curly braces and quotes need to be escaped properly.

D. Ivanov
  • 138
  • 10