在使用StringBuilder构建的JSON字符串中,为什么会出现语法错误:缺失

时间:2014-10-24 02:56:45

标签: asp.net json stringbuilder

我的字符串构建器,用于创建JSON Reply:

List<string> aList = getList();
StringBuilder StrBuilder = new StringBuilder();
StrBuilder.Append("\"length\":\"");
StrBuilder.Append(aList.Count.ToString());
int i = 1;
foreach (string str in aList)
{
   StrBuilder.Append("\", \"File" + i + "\":\"");
   StrBuilder.Append(str.ToString());
   i++;
}

我不知道错误在哪里,但我收到以下错误:

JSON - ")" missing error

1 个答案:

答案 0 :(得分:0)

List<string> aList = getList();
StringBuilder StrBuilder = new StringBuilder();
StrBuilder.Append("{\"length\":\"");
StrBuilder.Append(aList.Count.ToString());
int i = 1;
foreach (string str in aList)
{
   StrBuilder.Append("\", \"File" + i + "\":\"");
   StrBuilder.Append(str.ToString());
   i++;
}
StrBuilder.Append("\"}");

需要关闭“并需要添加{}。