3

I'm getting System.FormatException: Input string was not in a correct format when running this method.

The values of the fields:

arrayName = "requester";
fieldList = "\"name\" : \"shimshon\""; // "name" : "shimshon"


public override string ToString()
{
   var val = string.Format("\"{0}\" : { {1} }", arrayName, fieldList);

   return val;
}

the expect result of the method is

"requester" : { "name" : "shimshon" }

What is wrong with this format?

shimshon
  • 345
  • 3
  • 9
  • While this is not pertaining to the question at hand, from a general observation it appears you are attempting to create some kind of JSON object. I would recommend creating a class with the properties needed and use some kind of library such as JSON.net to serialize that into a JSON string, it will save . If that's not what you are attempting to do then ignore this comment. – Bearcat9425 Jul 31 '13 at 14:39

5 Answers5

7

I think you want:

var val = string.Format("\"{0}\" : {{ {1} }}", arrayName, fieldList);

Note the doubled {{ and }} which is the escape sequence necessary to get braces literally into the output.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
1

Try

var val = string.Format("\"{0}\" : {{ {1} }}", arrayName, fieldList);
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
1

Change:

public override string ToString()
{
    var val = string.Format("\"{0}\" : { {1} }", arrayName, fieldList);

    return val;
}

on:

public override string ToString()
{
   var val = string.Format("\"{0}\" : {{ {1} }}", arrayName, fieldList);

   return val;
}

The way escaped braces are interpreted can lead to unexpected results. For example, consider the format item "{{{0:D}}}", which is intended to display an opening brace, a numeric value formatted as a decimal number, and a closing brace. However, the format item is actually interpreted in the following manner:

  • The first two opening braces ("{{") are escaped and yield one opening brace.

  • The next three characters ("{0:") are interpreted as the start of a format item.

  • The next character ("D") would be interpreted as the Decimal standard numeric format specifier, but the next two escaped braces ("}}") yield a single brace. Because the resulting string ("D}") is not a standard numeric format specifier, the resulting string is interpreted as a custom format string that means display the literal string "D}".

  • The last brace ("}") is interpreted as the end of the format item.

  • The final result that is displayed is the literal string, "{D}". The numeric value that was to be formatted is not displayed.

Michael
  • 15,386
  • 36
  • 94
  • 143
0

You have to escape the { and } chars that are not part of the format.

string.Format("\"{0}\" : {{ {1} }}",.....)

See this MSDN page:

http://msdn.microsoft.com/en-us/library/txafckwd.aspx

Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}"). Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.

asawyer
  • 17,642
  • 8
  • 59
  • 87
0

You need to escape the curly brackets that you want to be curly brackets in your output by using double brackets:

public override string ToString()
{
   var val = string.Format("\"{0}\" : {{ {1} }}", arrayName, fieldList);

   return val;
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142