Asp.net Web服务返回json格式数据 [英] Asp.net Web service return json format data
本文介绍了Asp.net Web服务返回json格式数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我在asp.net编写了asp.net web服务并返回json格式数据
out put看起来像这样
< < span class =code-leadattribute> string xmlns = http://tempuri.org/ >
< script id = tinyhippos-inject / >
[{UserId:D0009,用户名:V.Gopalakrishnan ,CustID:122,CoName:Paras Telecom Pvt.Ltd。,DbName:ParEntSer86,LoginYn:1,XmlPath:null,FunctionID:7 EMAILID: 去pal@parastelecom.com,RoleId:null}]
< / string >
现在我想在android中使用这个网络服务申请和接收数据所以请帮助
怎么可能
解决方案
以下是我如何使用它工作正常
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;使用System.Web.Services
;
使用System.Web.Script.Serialization;
使用System.Web.Script.Services;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;使用System.ComponentModel
;
namespace Webservice
{
[WebService(Namespace =http://tempuri.org/)]
[WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web Service,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class Service1:System.Web.Services.WebService
{
public Service1()
{
//如果使用设计的组件,则取消注释以下行
// InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Void 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);
}
this.Context.Response.ContentType =application / json; charset = utf-8;
this.Context.Response.Write(serializer.Serialize(new {Cargo = rows}));
}
public string errmsg(Exception ex)
{
return[['ERROR','+ ex.Message +']];
}
}
}
引用:这是我的输出:(我没有得到xml字符串)
{Cargo:[{Id:1,FirstName:devi,LastName:priya,Contactno:965577796},{Id:2,FirstName:阿伦, 名字: 库马尔, Contactno: 9944142109},{ ID:3 姓: 卡鲁, 名字: 罗纳德, Contactno: 8883205008 }]}
Hello
i've written asp.net web service in asp.net and return json format data
out put look like this
<string xmlns="http://tempuri.org/">
<script id="tinyhippos-injected"/>
[{"UserId":"D0009 ","Username":"V. Gopalakrishnan ","CustID":"122","CoName":"Paras Telecom Pvt.Ltd.","DbName":"ParEntSer86","LoginYn":"1","XmlPath":null,"FunctionID":"7","EmailId":"gopal@parastelecom.com","RoleId":null}]
</string>
and now i wnt to use this web service in android application and receive data so please help
how can this possible
解决方案
Here is how i used it was working fine
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Serialization; using System.Web.Script.Services; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.ComponentModel; namespace Webservice { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { public Service1() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public Void 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); } this.Context.Response.ContentType = "application/json; charset=utf-8"; this.Context.Response.Write(serializer.Serialize(new { Cargo = rows })); } public string errmsg(Exception ex) { return "[['ERROR','" + ex.Message + "']]"; } } }
Quote:Here is my output:(i'm not getting xml strings)
{"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"}]}
这篇关于Asp.net Web服务返回json格式数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文