我只是在尝试为我的单元测试构建一个json字符串,并且意外地,以下代码返回系统格式异常。错误消息表明它正在尝试解析日期,这对我来说很奇怪。我不是要解析日期。
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetJson());
Console.ReadKey();
}
static string GetJson(string dateStr = "", string lta = "5.25")
{
return String.Format("[{\"dateBooking\":\"{0}\",\"lta\":\"{1}\"}]", dateStr, lta);
}
}
Run Code Online (Sandbox Code Playgroud)
可以很容易地复制它,但是我要添加异常详细信息:
“ mscorlib.dll中发生了类型为'System.FormatException'的未处理的异常
其他信息:输入字符串的格式不正确。”
您需要对{with {{和}with 进行转义,}}因为String.Format它将搜索类似的参数,{0:000}但会查找{"dateBooking ... }无效的参数格式。这就是为什么引发FormatException的原因。
return String.Format("[{{\"dateBooking\":\"{0}\",\"lta\":\"{1}\"}}]", dateStr, lta);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2885 次 |
| 最近记录: |