如何从asmx服务menthods中的数据表中获取json数组。
我这样编码但是没有进入数组
[WebMethod(CacheDuration = 500)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public String latency(int testId)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("GetValues", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add(new SqlParameter("TestId", testId));
DataTable dt=new DataTable();
da.Fill(dt);
string[][] JaggedArray = new string[dt.Rows.Count][];
int i = 0;
foreach (DataRow rs in dt.Rows)
{
JaggedArray[i] = new string[] { rs["Time"].ToString(), rs["minlatency"].ToString() };
i = i + 1;
}
string json = JsonConvert.SerializeObject(JaggedArray);
return json;
}
我的回答是
{“d”:“[[\”2/3/2012 11:30:14 AM \“,\”10 \“],[\”2/3/2012 11:30:16 AM \“ ,\“5 \”],[\“2/3/2012 11:30:18 AM \”,\“7 \”]]“}
答案 0 :(得分:0)
您可以尝试.NET Framework的JavaScriptSerializer:
JavaScriptSerializer serializer = new JavaScriptSerializer();
var serialized = serializer.Serialize(
new int[][]
{
new []{1,2,3},
new []{4,5,6}
}
);
Console.WriteLine(serialized);
Console.ReadLine();
输出:
[[1,2,3],[4,5,6]]