0

I am using httpwebrequest to communicate with a restful service and I need to send a json. I want the json format to be configurable therefore I am trying to use a string template and format:

string template ="{ \"content\" : \"{0}\" }";
string.format(template,"my data");

But I get an exception of bad format. Any ideas on how to.create a template containing curly brackets?

Thanks ahead, Alon

alostr
  • 170
  • 2
  • 2
  • 11

2 Answers2

3

You have to escape brackets as explained here on SO and on MSDN:

string template ="{{ \"content\" : \"{0}\" }}";
Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
2

This should work:

string template ="{{ \"content\" : \"{0}\" }}";
string.Format(template,"my data");

You need to escape the braces that are not used to specify a parameter.

Alberto
  • 15,626
  • 9
  • 43
  • 56