0

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
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • 4
    `{` and `}` are special to String.Format. You will have to escape them all to `{{` or `}}` if you want actual braces in the output. – Mike Zboray Jul 30 '14 at 08:41
  • probably better to just concatenate the values when building the JSON. Or maybe make your own `Format()` that uses `Replace()`... e.g. `json.Replace("{0}", key1);` – musefan Jul 30 '14 at 08:43
  • Do not take the pain and concatenate JSON using string.Format or similar. Use a JSON serializer instead. – chiccodoro Jul 30 '14 at 11:03

0 Answers0