繁体   English   中英

将DateTime.toString()传递给AppendFormat C#.NET时发生FormatException

[英]FormatException when passing DateTime.toString() to AppendFormat C# .NET

我正在尝试执行以下操作:

 var policyBuilder = new StringBuilder();    

 var expiration = DateTime.UtcNow.AddDays(1).ToString("s") + "Z";

 policyBuilder.AppendFormat("{ \"expiration\": \"{0}\",\n", expiration);

但是,最后一行抛出以下异常:

 An exception of type 'System.FormatException' occurred in mscorlib.dll 
 but was not handled in user code

 Additional information: Input string was not in a correct format.

'expiration'是一个字符串,那么为什么会出现此错误?

谢谢

如果想在开始时使用{ ,则必须使用两个:

policyBuilder.AppendFormat("{{ \"expiration\": \"{0}\",\n", 10);

请参阅: 以复合格式转义大括号

左括号和右括号被解释为格式项的开始和结束。 因此,必须使用转义序列来显示文字的右括号或右括号。 在固定文本中指定两个开括号(“ {{”)以显示一个开括号(“ {”) ,或两个闭括号(“}}”)以显示一个闭括号(“}”)。 格式项中的大括号将按其遇到的顺序顺序进行解释。 不支持解释嵌套括号。 ....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM