c# 在 while 循环中使用 JavaScriptSerializer 序列化程序并返回到客户端 Json.Parse。出现错误
c# using JavaScriptSerializer serializer in while loop and returning to client side to Json.Parse. Getting a error
谁能给我一个步骤来解决这个获取输出的错误。编码新手。
步骤 1:
使用 Json/Javascript 调用 .cs 文件中的方法 (c#)
var markers = JSON.parse('<%=ConvertDataTabletoString("NewGetTaskWorkPercentage1",null) %>');
步骤 2:
.cs file code for method ConvertDataTabletoString
public string ConvertDataTabletoString(string Sql, string prjno)
{
//var qprojectno = Request.QueryString["ProjectNo"].ToString();
param1.Value = "P-2020-0009"; //aspx hidden field
DataTable dt = new DataTable();
using (con = new SqlConnection(connectionString))
{
using (cmd = new SqlCommand(Sql, con))
{
string connetionString = "Data Source = ; Initial Catalog = DashBoardDB; User ID = ; Password = ";
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();
string sql1 = "SELECT InstanceId,Module,SubModule,Process FROM Xtable WHERE ProjectNo=" + "'" + param1.Value + "'"+ "and instanceid='aabed4df-3e91-41c0-8788-2e7d31eecfd8'";
SqlCommand command = new SqlCommand(sql1, connection);
command.CommandTimeout = 600;
SqlDataReader dataReader = command.ExecuteReader();
serializedResult = " ";
while (dataReader.Read())
{
string strInsId = " ";
string strProcess = "";
strInsId = dataReader["InstanceId"].ToString();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@ProjectNo", prjno));
cmd.Parameters.Add(new SqlParameter("@InstanceId", strInsId));
sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine(dr + " " + dt.Rows);
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
serializedResult = serializer.Serialize(rows);
serializedResult += serializer.Serialize(rows); **two times concatenating**
con.Close();}
dataReader.Close();
command.Dispose();
connection.Close();
return serializedResult;
}
}
}
步骤 3:
附加图片
在客户端如何将这些值拆分成数组。我需要这样的输出。我怎样才能得到数组中的输出和客户端数组的长度。谢谢
数组 1:
[{"Task":"Feature 1.1.3","OverallSubtaskDuration":17.00,"ActualStart":"2019/10/18","ActualEnd":"2020/03/13","OverallSubtaskPercentage":283.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":8.00,"ActualStart":"2019/10/18","ActualEnd":"2019/10/25","OverallSubtaskPercentage":133.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2020/03/11","ActualEnd":"2020/03/13","OverallSubtaskPercentage":50.00},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2019/10/27","ActualEnd":"2019/10/29","OverallSubtaskPercentage":50.00}]
数组 2:
[{"Task":"Feature 1.1.3","OverallSubtaskDuration":17.00,"ActualStart":"2019/10/18","ActualEnd":"2020/03/13","OverallSubtaskPercentage":283.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":8.00,"ActualStart":"2019/10/18","ActualEnd":"2019/10/25","OverallSubtaskPercentage":133.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2020/03/11","ActualEnd":"2020/03/13","OverallSubtaskPercentage":50.00},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2019/10/27","ActualEnd":"2019/10/29","OverallSubtaskPercentage":50.00}]
JSON 结果必须是对象或数组。
您创建的结果是两个背靠背的数组,因此无效 JSON,您可以使用 https://jsonlint.com/
等在线验证器来测试它
因此,您要么需要使用其他两个数组中的所有元素创建一个大数组,要么需要将这两个数组包装到一个包装器对象中。
因为您已经在使用 List<>,所以对您来说最简单的方法是将其与自身结合并 return 结果。
//Add the "rows" list to itself, so you double it.
rows.AddRange(rows);
serializedResult = serializer.Serialize(rows);
...
return serializedResult;
如果您需要保持列表不同,则必须使用包装器对象。像这样;
public class Wrapper
{
public List<Dictionary<string, object>> Array1 { get; set; }
public List<Dictionary<string, object>> Array2 { get; set; }
}
然后你这样连载
serializedResult = serializer.Serialize(new Wrapper() { Array1 = rows, Array2 = rows };);
...
return serializedResult;
您可以使用 newtonsoft json dll 从 .cs 创建 json 文件,并使用 link 检查 json 字符串:https://jsoneditoronline.org/
谁能给我一个步骤来解决这个获取输出的错误。编码新手。
步骤 1: 使用 Json/Javascript 调用 .cs 文件中的方法 (c#)
var markers = JSON.parse('<%=ConvertDataTabletoString("NewGetTaskWorkPercentage1",null) %>');
步骤 2:
.cs file code for method ConvertDataTabletoString
public string ConvertDataTabletoString(string Sql, string prjno)
{
//var qprojectno = Request.QueryString["ProjectNo"].ToString();
param1.Value = "P-2020-0009"; //aspx hidden field
DataTable dt = new DataTable();
using (con = new SqlConnection(connectionString))
{
using (cmd = new SqlCommand(Sql, con))
{
string connetionString = "Data Source = ; Initial Catalog = DashBoardDB; User ID = ; Password = ";
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();
string sql1 = "SELECT InstanceId,Module,SubModule,Process FROM Xtable WHERE ProjectNo=" + "'" + param1.Value + "'"+ "and instanceid='aabed4df-3e91-41c0-8788-2e7d31eecfd8'";
SqlCommand command = new SqlCommand(sql1, connection);
command.CommandTimeout = 600;
SqlDataReader dataReader = command.ExecuteReader();
serializedResult = " ";
while (dataReader.Read())
{
string strInsId = " ";
string strProcess = "";
strInsId = dataReader["InstanceId"].ToString();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@ProjectNo", prjno));
cmd.Parameters.Add(new SqlParameter("@InstanceId", strInsId));
sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine(dr + " " + dt.Rows);
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
serializedResult = serializer.Serialize(rows);
serializedResult += serializer.Serialize(rows); **two times concatenating**
con.Close();}
dataReader.Close();
command.Dispose();
connection.Close();
return serializedResult;
}
}
}
步骤 3: 附加图片
在客户端如何将这些值拆分成数组。我需要这样的输出。我怎样才能得到数组中的输出和客户端数组的长度。谢谢
数组 1:
[{"Task":"Feature 1.1.3","OverallSubtaskDuration":17.00,"ActualStart":"2019/10/18","ActualEnd":"2020/03/13","OverallSubtaskPercentage":283.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":8.00,"ActualStart":"2019/10/18","ActualEnd":"2019/10/25","OverallSubtaskPercentage":133.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2020/03/11","ActualEnd":"2020/03/13","OverallSubtaskPercentage":50.00},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2019/10/27","ActualEnd":"2019/10/29","OverallSubtaskPercentage":50.00}]
数组 2:
[{"Task":"Feature 1.1.3","OverallSubtaskDuration":17.00,"ActualStart":"2019/10/18","ActualEnd":"2020/03/13","OverallSubtaskPercentage":283.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":8.00,"ActualStart":"2019/10/18","ActualEnd":"2019/10/25","OverallSubtaskPercentage":133.33},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2020/03/11","ActualEnd":"2020/03/13","OverallSubtaskPercentage":50.00},
{"Task":"Feature 1.1.3","OverallSubtaskDuration":3.00,"ActualStart":"2019/10/27","ActualEnd":"2019/10/29","OverallSubtaskPercentage":50.00}]
JSON 结果必须是对象或数组。
您创建的结果是两个背靠背的数组,因此无效 JSON,您可以使用 https://jsonlint.com/
等在线验证器来测试它因此,您要么需要使用其他两个数组中的所有元素创建一个大数组,要么需要将这两个数组包装到一个包装器对象中。
因为您已经在使用 List<>,所以对您来说最简单的方法是将其与自身结合并 return 结果。
//Add the "rows" list to itself, so you double it.
rows.AddRange(rows);
serializedResult = serializer.Serialize(rows);
...
return serializedResult;
如果您需要保持列表不同,则必须使用包装器对象。像这样;
public class Wrapper
{
public List<Dictionary<string, object>> Array1 { get; set; }
public List<Dictionary<string, object>> Array2 { get; set; }
}
然后你这样连载
serializedResult = serializer.Serialize(new Wrapper() { Array1 = rows, Array2 = rows };);
...
return serializedResult;
您可以使用 newtonsoft json dll 从 .cs 创建 json 文件,并使用 link 检查 json 字符串:https://jsoneditoronline.org/