iam新增了wcf。 「:\」 Sathyam IAM通過使用下面的代碼如何在wcf中返回json格式
public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=mar-pc;database=user;User ID=sa;Password=123123;"))
{
using (SqlCommand cmd = new SqlCommand("select title=tname,tid=taddress from userdetails where userid='1'", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.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)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
其中IAM獲得結果作爲
「[{\」 標題\由序列化爲JSON格式顯影WCF寧靜
IAM返回字符串\ 「\ 」TID \「:\ 」尼扎馬巴德\「}]」
我想要得到的結果作爲
{ 「稱號」: 「Sathyam」, 「TID」: 「尼扎馬巴德」}
IAM承包經營和
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "MyGetData/{value}/{password}")]
幫助我,我試圖bodystyle如裹也沒有
但是您有'List'。那是你的輸出。你爲什麼要改變它? –
2014-11-06 08:44:26
我想得到的輸出爲{「title」:「Sathyam」,「tid」:「Nizamabad」}所以我應該怎麼做不知道如何輸出將是 –
user2765331
2014-11-06 08:49:26