[英]get data from sql server database in json using wcf,c#
I am able to get json data from a SQL Server table but the format of json seems to be in incorrect. 我可以从SQL Server表中获取json数据,但是json的格式似乎不正确。
There is extra " " after json: json之后还有额外的“”:
"[{\"deviceid\":\"jafjajf17841278947\"},
{\"deviceid\":\"ahfaj2528\"},
{\"deviceid\":\"hefhsf9872987572\"},
{\"deviceid\":\"22\"},
{\"deviceid\":\"23\"}]"
Inverted commas at end and beginning of json Now problem is that when we call this json is called in android and browser we get these inverted quotes but not in fiddler. json开头和结尾处的反逗号现在问题是,当我们在android和浏览器中调用此json时,会得到这些反引号,但在小提琴手中却没有。 Do i need to make changes in code. 我需要对代码进行更改吗?
Code I tried: 我试过的代码:
GetDeviceId.svc.cs
: GetDeviceId.svc.cs
:
public string GetDeviceIds()
{
try
{
MySqlCommand command = default(MySqlCommand);
MySqlDataAdapter adaptor = new MySqlDataAdapter();
DataTable dt = new DataTable();
string sql = "select deviceid from userreg";
digitalindia.Open();
command = new MySqlCommand(sql, digitalindia);
adaptor.SelectCommand = command;
adaptor.Fill(dt);
adaptor.Dispose();
command.Dispose();
digitalindia.Close();
if (dt.Rows.Count > 0)
{
string json = GetJson(dt);
//var j = JsonConvert.SerializeObject(json);
//j = j.Substring(2);
//json
//string json = GetJson(dt);
//json.Remove(1, json.Length - 1);
//var json1 = EvaluateException(json);
//ArrayList json = new ArrayList();
//ArrayList json = new ArrayList();
//json.Add(DataTableToJsonWithStringBuilder(dt));
return json;
//return string.Format("You entered: {0}", json); ;
}
else
{
string json ="null";
return json;
//return "No Party Found";
}
}
catch (Exception ex)
{
//ArrayList json = new ArrayList();
//return ex;
return ex.Message.ToString();
}
}
private string GetJson(DataTable dt)
{
System.Web.Script.Serialization.JavaScriptSerializer Jserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rowsList = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rowsList.Add(row);
}
return Jserializer.Serialize(rowsList);
}
IGetDeviceId.svc
: IGetDeviceId.svc
:
[ServiceContract]
public interface IGetDeviceId
{
[OperationContract()]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetDeviceId")]
string GetDeviceIds();
}
you can use this code :its take a query dynamically and return json 您可以使用此代码:它动态获取查询并返回json
public List<List<string>> GetDataAsList(string query)
{
try
{
intializeConnection();
DataTable dataTable;
DataSet dataset = new DataSet();
List<List<string>> data;
query = query.Trim().ToUpper();
DataRow row = null;
string[] commasCount;
string[] commasCountWithoutFrom;
if (query.Contains('*'))
{
return null;
}
commasCount = query.Trim().Split(new string[] { "FROM" }, StringSplitOptions.None);
string commasString = commasCount[0].Trim();
commasCountWithoutFrom = commasString.Trim().Split(',');
ArrayList columnsName2 = new ArrayList();
for (int c = 0; c < commasCountWithoutFrom.Length; c++)
{
string raw = commasCountWithoutFrom[c].Trim();
string[] parts = raw.Trim().Split(' ');
string sub_parts = parts[parts.Length - 1];
columnsName2.Add(sub_parts.ToString());
}
List<List<string>> resultArr=null;
string cmdStr = String.Format(query);
command = new SqlCommand(cmdStr, connection);
set = new DataSet();
adapter = new SqlDataAdapter(command);
adapter.Fill(set);
if (set.Tables[0].Rows.Count > 0)
{
resultArr = new List<List<string>>();
for (int i = 0; i < set.Tables[0].Rows.Count; i++)
{
resultArr.Add(new List<string>());
for (int col = 0; col < columnsName2.Count; col++)
{
resultArr[i].Add(set.Tables[0].Rows[i][columnsName2[col].ToString()].ToString().Trim());
}
}
}
else
{
resultArr = new List<List<string>>();
resultArr.Add(new List<string>());
for (int col = 0; col < columnsName2.Count; col++)
{
resultArr[0].Add("No Data");
}
}
connection.Close();
return resultArr;
}
catch (SystemException ex)
{
connection.Close();
return null;
}
}
and use this one in ISERVICE.cs 并在ISERVICE.cs中使用它
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetDataAsList/{query}")]
List<List<string>> GetDataAsList(string query);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.