Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用string.format时获取输入字符串的格式不正确_C#_Asp.net_.net_C# 4.0 - Fatal编程技术网

C# 使用string.format时获取输入字符串的格式不正确

C# 使用string.format时获取输入字符串的格式不正确,c#,asp.net,.net,c#-4.0,C#,Asp.net,.net,C# 4.0,我正在尝试使用string.format填充字符串模板中的一些值,以下是字符串值: { \"version\":\"9.40.20153.0\",\"sheetCount\":1, \"sheets\":{ \"{0}\": { \"name\":\"{1}\", \"rowCount\":{2}, \"columnCount\":{3}, \"colHeaderData\":{ \"dataTable\":{ {4} } }, \"data\":{ \"dataTable\"

我正在尝试使用string.format填充字符串模板中的一些值,以下是字符串值:

{
\"version\":\"9.40.20153.0\",\"sheetCount\":1,
\"sheets\":{

\"{0}\":
{
\"name\":\"{1}\",
\"rowCount\":{2},
\"columnCount\":{3},
\"colHeaderData\":{
\"dataTable\":{
{4}
}
        },
\"data\":{
\"dataTable\":{
                 {5}
        },
    \"index\":0
    }
  }
}
下面是调用string.Format方法的代码:

string newString=SB.AppendLine(string.Format(genericTemplate, sheetName,columnCount, rowCount,3,5,6)).ToString();

现在它变得非常令人沮丧:(.请帮助!!

你必须避开所有不用于格式化的花括号。 这是通过简单地复制每个花括号来完成的

例如,第一行和最后一行应如下所示:

{{
}}
虽然这条线必须保持原样:

\"{0}\":

var genericTemplate = @"
{{
""version"":""9.40.20153.0"",""sheetCount"":1,
""sheets"":{{

""{0}"":
{{
""name"":""{1}"",
""rowCount"":{2},
""columnCount"":{3},
""colHeaderData"":{{
""dataTable"":{{
{4}
}}
        }},
""data"":{{
""dataTable"":{{
                 {5}
        }},
    ""index"":0
    }}
  }}
}}";
var newString = string.Format(genericTemplate, "arg1", "arg2", "arg3", "arg4", "arg5", "arg6");

发生这种情况是因为您试图在字符串中使用大括号。您需要像另一个答案所说的那样对其进行转义。更好的选择可能是为此创建一个对象并将其序列化为JSON。

太好了,它解决了这个问题:D