在结果中添加单词 [英] adding the word in the result
本文介绍了在结果中添加单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在创建一个webservice,它应该从数据库(sql server)获取数据并将其返回。
每件事都很好。但是我需要的是我需要以我需要的格式显示数据。
这是我的代码:
Hi
I'm creating a webservice which should get data from database(sql server) and return it.
Every thing working fine.But what i need is i need to display the data with the format which i need.
Here is my code:
public string GetEmployees()
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Contact e ";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection = con;
da.Fill(dt);
con.Close();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow rs in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, rs[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
public string errmsg(Exception ex)
{
return "[['ERROR','" + ex.Message + "']]";
}
以上代码的结果是:
The result for the above code is:
[{"Id":1,"FirstName":"devi","LastName":"priya","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},
{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]
我需要的只是{货物这个词:最后我的结果}
这是我期望的结果
What i need is just the word {"cargo": and my result finally }
Here is the result which i expect
{ "Cargo": [{"Id":1,"FirstName":"devi","LastName":"priya","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},
{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}
任何人都可以帮我解决这个问题。
在此先感谢。
can any one please help me to solve this.
Thanks in advance.
推荐答案
而不是return serializer.Serialize(rows);
i改为:
返回{\Cargo \:+序列化器.Serialize(rows)+};
分享|编辑|标志
instead of return serializer.Serialize(rows);
i changed it as:
return "{ \"Cargo\": " + serializer.Serialize(rows) + "}";
share|edit|flag
这篇关于在结果中添加单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文