1

I have problem in formatting json string in c#

I have following two lines

string formater = @"{'attachments':[{'title':'New item added','title_link':{0},'text':{1},'image_url':{2},'color':'#764FA5'}]}";
string myJson = String.Format(formater, product.Url, "text", product.ImageUrl);

I'm getting this Exception

System.FormatException: Input string was not in a correct format.

I think because of curly braces inside formatting string, how can i escape?

Bakurits
  • 402
  • 3
  • 11

1 Answers1

2

Use double curly braces Escape curly brace '{' in String.Format

Oh, and you will need to add quotes around you placeholders:

string formater = @"{{'attachments':[{{'title':'New item added','title_link':'{0}','text':'{1}','image_url':'{2}','color':'#764FA5'}}]}}";
string myJson = String.Format(formater, product.Url, "text", product.ImageUrl);
AaronHolland
  • 1,595
  • 1
  • 16
  • 32