-3

I am getting this error with the following line of code,

int numberStored = 9;
record.VALUE = string.Format("{\"FIELDS\":[{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}]}", numberStored.ToString(), 0);

This works fine if i substitute out {0} and place the 9 in directly, but obviously I don't want it hard coded like this. The previous answers I can see for this problem do not seem to be helping me.

user3407039
  • 1,285
  • 5
  • 25
  • 51
  • As an aside, you don't need `numberStored.ToString()`, you can use `numberStored` in `String.Format`. `ToString` is called implicitly anyway. – Tim Schmelter Mar 11 '15 at 15:21
  • Please instead of just typing "The previous answers I can see for this problem do not seem to be helping me." into the question as "demonstration" of research add actual links to questions you've found. I.e. "I've searched for http://www.bing.com/search?q=c%23+string+format+curly which gave me http://stackoverflow.com/questions/3773857/escape-curly-brace-in-string-format, but I don't get what 'Use double braces' mean" or something similar. – Alexei Levenkov Mar 11 '15 at 15:28

1 Answers1

8

You have to escape curly braces by doubling them:

int numberStored = 9;
record.VALUE = string.Format("{{\"FIELDS\":[{{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}}]}}", numberStored.ToString(), 0);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
dario
  • 5,149
  • 12
  • 28
  • 32