我已经创建了一个返回JSON格式数据的WCF Web服务。我的代码如下所示:
我得到了以下输出:
{ "SampleServiceResult": "{\"Id\":\"1\",\"Name\":\"xyz\",\"email\":\"xya@test.com\"}" }
但我想要以下输出:
{ "SampleServiceResult": {"Id":"1","Name":"xyz","email":"xya@test.com"} }
在输出中它添加了 "\" 转义字符。如何删除它并返回有效的 JSON?
我已经尝试替换 "\",但没有成功。
谢谢
String sJSONdata = "";
StreamReader reader = new StreamReader(data);
sJSONdata = reader.ReadToEnd();
//'now convert the JSON into a data table
DataTable dt = GetJSONTable(sJSONdata);
dt.TableName = "Customer";
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (DataRow rs in dt.Rows)
{
dict = new Dictionary<string, string>();
foreach (DataColumn col in dt.Columns)
{
dict.Add(col.ColumnName, rs[col].ToString());
}
}
return (new JavaScriptSerializer().Serialize(dict));
我得到了以下输出:
{ "SampleServiceResult": "{\"Id\":\"1\",\"Name\":\"xyz\",\"email\":\"xya@test.com\"}" }
但我想要以下输出:
{ "SampleServiceResult": {"Id":"1","Name":"xyz","email":"xya@test.com"} }
在输出中它添加了 "\" 转义字符。如何删除它并返回有效的 JSON?
我已经尝试替换 "\",但没有成功。
谢谢
GetJSONTable()
方法返回了单列,而您似乎期望多列?正如我上面所说的,将其序列化为与数据匹配的类,而不是尝试使用datatable,除非数据变化很大。 - GPW