C# System.format
does not work with JSON String
I want to generate multiple JSON string
by changing only certain fields.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String working = "Hello there {0} {1} {2} {3} {4}";
int key = 100;
Console.WriteLine(String.Format(working, key, key + 1, key + 2, key + 3, key + 4));
String notWorking = "{ " +
" \"store\": " +
" {" +
" \"book\": " +
" [ " +
" { " +
" \"category\": \"reference\"," +
" \"author\": \"Nigel Rees\"," +
" \"title\": \"Sayings of the Century\"," +
" \"price\": {0}" +
" }," +
" { " +
" \"category\": \"fiction\"," +
" \"author\": \"Evelyn Waugh\"," +
" \"title\": \"Sword of Honour\"," +
" \"price\": {1}" +
" }," +
" { " +
" \"category\": \"fiction\"," +
" \"author\": \"Herman Melville\"," +
" \"title\": \"Moby Dick\"," +
" \"isbn\": \"0-553-21311-3\"," +
" \"price\": {2}" +
" }," +
" { " +
" \"category\": \"fiction\"," +
" \"author\": \"J. R. R. Tolkien\"," +
" \"title\": \"The Lord of the Rings\"," +
" \"isbn\": \"0-395-19395-8\"," +
" \"price\": {3}" +
" }" +
" ]," +
" \"bicycle\": " +
" {" +
" \"color\": \"red\"," +
" \"price\": {4}" +
" }" +
"}" +
"} ";
int key1 = 100;
Console.WriteLine(String.Format(notWorking, key1, key1 + 1, key1 + 2, key1 + 3, key1 + 4));
}
}
}
Getting following error
Hello there 100 101 102 103 104
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.String.Format(String format, Object[] args)
at ConsoleApplication1.Program.Main(String[] args) in c:\users\adongre\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 57