Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用wcf、c从json格式的sql server数据库获取数据#_C#_Json_Wcf - Fatal编程技术网

C# 使用wcf、c从json格式的sql server数据库获取数据#

C# 使用wcf、c从json格式的sql server数据库获取数据#,c#,json,wcf,C#,Json,Wcf,我能够从SQLServer表中获取json数据,但json的格式似乎不正确 json后面还有一个额外的“”: "[{\"deviceid\":\"jafjajf17841278947\"}, {\"deviceid\":\"ahfaj2528\"}, {\"deviceid\":\"hefhsf9872987572\"}, {\"deviceid\":\"22\"}, {\"deviceid\":\"23\"}]" json结尾和开头的倒逗号 现在的问题是,当我们在android和浏

我能够从SQLServer表中获取json数据,但json的格式似乎不正确

json后面还有一个额外的“”:

"[{\"deviceid\":\"jafjajf17841278947\"},
 {\"deviceid\":\"ahfaj2528\"},
 {\"deviceid\":\"hefhsf9872987572\"},
 {\"deviceid\":\"22\"},
 {\"deviceid\":\"23\"}]" 
json结尾和开头的倒逗号 现在的问题是,当我们在android和浏览器中调用这个json时,我们会得到这些倒引号,但在fiddler中不会。 我是否需要在代码中进行更改

我试过的代码:

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);
}

您可以使用以下代码:它动态地接受查询并返回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;
        }
    }
公共列表GetDataAsList(字符串查询) { 尝试 { 初始化连接(); 数据表数据表; 数据集=新数据集(); 列出数据; query=query.Trim().ToUpper(); DataRow行=null; 字符串[]commasCount; 字符串[]commascountwithout from; if(query.Contains('*')) { 返回null; } commasCount=query.Trim().Split(新字符串[]{“FROM”},StringSplitOptions.None); 字符串commasString=commasCount[0]。Trim(); commasCountWithoutFrom=commasString.Trim().Split(','); ArrayList columnsName2=新的ArrayList(); for(int c=0;c0) { resultar=新列表(); 对于(int i=0;i 在ISERVICE.cs中使用这个

 [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetDataAsList/{query}")]

    List<List<string>> GetDataAsList(string query);
[WebInvoke(Method=“GET”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate=“GetDataAsList/{query}”)]
列表GetDataAsList(字符串查询);

你确定吗?或者这只是另一个调试视图?amit,它在fiddler中正确显示。尝试从浏览器访问URL,看看会发生什么。在URL中,我得到json,但介于“”之间,因此我们必须删除android中调用此服务的倒逗号。我们可以在代码中删除这些“”(wcf服务由c代码生成)。或者我们只能在android应用程序中这样做。链接到服务:gps.traxistar.net/GetDeviceId.svc/GetDeviceId
 [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetDataAsList/{query}")]

    List<List<string>> GetDataAsList(string query);