[英]Escaping arguments for string.Format in a C# multiline verbatim string
[英]Unable to string.Format a C# multiline verbatim string when escaping curly braces
我在带有转义大括号的逐字字符串上遇到String.Format问题。
引发FormatError() Exception:Message: System.FormatException : Input string was not in a correct format.
String s = $@"{{ ""ver"": ""1.0"",""userId"": ""{0}""}}";
String.Format(s, "1234")
您正在使用C#字符串插值特殊字符“ $”,但是,您在模板中使用了位置参数。
它应该是:-
String s = @"{{ ""ver"": ""1.0"",""userId"": ""{0}""}}";
String.Format(s, "1234").Dump();
要不就:-
var userId = 1234;
String s = $@"{{ ""ver"": ""1.0"",""userId"": ""{userId}""}}";
如果您打算生成JSON输出,则更合适的方法是创建您的对象并使用Newtonsoft.Json
包将其序列化:
var x = new
{
ver = "1.0",
userId = "1234"
};
var s = JsonConvert.SerializeObject(x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.