使用wcf,c#从json中的sql server数据库获取数据 [英] get data from sql server database in json using wcf,c#

查看:131
本文介绍了使用wcf,c#从json中的sql server数据库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



json之后还有额外的:



[{\deviceid \:\jafjajf17841278947 \},

{\deviceid \\ \\:\ahfaj2528 \},

{\deviceid \:\hefhsf9872987572 \},

{\ deviceid \:\22 \},

{\deviceid \:\23 \}]



json结束和开头的反转逗号

现在的问题是,当我们调用这个json在android和浏览器中被调用时,我们得到这些反向引号但不是在fiddler中。 />
我是否需要更改代码。



代码我试过:



`GetDeviceId.svc.cs`:



  public   string  GetDeviceIds()
{
try
{
MySqlCommand command = default (MySqlCommand);
MySqlDataAdapter adapter = new MySqlDataAdapter();

DataTable dt = new DataTable();
string sql = 从userreg中选择deviceid ;

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();
< span class =code-comment> //
ArrayList json = new ArrayList();
// json.Add(DataTableToJsonWithStringBuilder(dt));
return json;
// return string.Format(你输入:{0},json); ;
}
else
{
string json = null;
return json;
// 返回找不到派对;
}
}
catch (例外情况)
{
// < span class =code-comment> ArrayList json = new ArrayList();
// return ex;
return ex.Message.ToString();
}
}

私人 字符串 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 字典< string,object>();

foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName,dr [col]);
}

rowsList.Add(row);
}

return Jserializer.Serialize(rowsList);
}



`IGetDeviceId.svc`:



 [ServiceContract] 
public interface IGetDeviceId
{
[OperationContract()]
[WebInvoke(Method = GET,ResponseFormat = WebMessageFormat .Json,BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = GetDeviceId)]
string GetDeviceIds();
}

解决方案

尝试使用

 JsonConvert.SerializeObject(rowsList); 

而不是

 Jserializer.Serialize(rowsList);; 



以下链接可能有助于解决问题。

< a href =http://stackoverflow.com/questions/31900925/console-shows-correctly-formatted-json-but-jsonobject-is-unable-to-parse-it> http://stackoverflow.com/questions / 31900925 / console-shows-correct-formatted-json-but-jsonobject-is-could-to-parse-it [ ^ ]

http://stackoverflow.com/questions/6620165/how-can-i-parse- json-with-c [ ^ ]

ASP.NET MVC 5中的WW RESTful服务和WebGrid - 第1部分 [ ^ ]


I am able to get json data from a SQL Server table but the format of json seems to be in incorrect.

There is extra " " after 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.
Do i need to make changes in code.

Code I tried:

`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`:

[ServiceContract]
    public interface IGetDeviceId
    {
        [OperationContract()]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetDeviceId")]
        string GetDeviceIds();       
    }

解决方案

Try with

JsonConvert.SerializeObject(rowsList);

instead of

Jserializer.Serialize(rowsList);;


Following links may help in resolving the issue.
http://stackoverflow.com/questions/31900925/console-shows-correctly-formatted-json-but-jsonobject-is-unable-to-parse-it[^]
http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c[^]
WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 1[^]


这篇关于使用wcf,c#从json中的sql server数据库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆