如何忽略Json webservice输出中的默认值(字符串)。 [英] how to ignore default value(string) in the output of Json webservice.
问题描述
嗨
我需要在输出中显示默认值(字符串),该值在执行webservice JSON时显示。
这是我的代码:
使用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
;
名称空间Webservice
{
///< summary>
/// Service1
///< / summary>的摘要说明
[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 string GetEmployees(string SearchTerm)
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);
}
返回{\Cargo \:+ serializer.Serialize(rows)+};
}
public string errmsg(Exception ex)
{
return[['ERROR','+ ex.Message +']];
}
}
}
输出为:
< string>
{Cargo:[{Id:1,FirstName:devi,LastName:priya,Contactno:965577796},{Id:2,姓 : 阿伦, 名字: 库马尔, Contactno: 9944142109},{ ID:3 姓: 卡鲁, 名字: 罗纳德, Contactno :8883205008}]}
< / string>
结果应如下:
{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}]}
在上面的输出中我不想看到字符串/字符串
有没有办法省略,如果是这样请建议我。
提前致谢
hi
上述问题的解决方案是
1. ofpublic string GetEmployees()
我将其更改为
public Void Ge tEmployees()
2.而不是返回结果我使用以下代码
this .Context.Response.ContentType = application / json; charset = utf-8;
this .Context.Response.Write(serializer.Serialize( new {Cargo = rows}));
一旦我这样做了,我得到了我真正想要的结果:
这是我的输出:(我没有得到xml字符串)
{Cargo:[{Id:1,FirstName:devi,LastName:priya,Contactno:965577796},{Id:2,FirstName : 阿伦, 名字: 库马尔, Contactno: 9944142109},{ ID:3 姓: 卡鲁, 名字: 罗纳德, Contactno: 8883205008}]}
Hi
I need to negelct the default value(string) in the output which is displayed when executing the webservice JSON.
Here is my code:
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 { /// <summary> /// Summary description for Service1 /// </summary> [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 string GetEmployees(string SearchTerm) 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 "{ \"Cargo\": " + serializer.Serialize(rows) + "}"; } public string errmsg(Exception ex) { return "[['ERROR','" + ex.Message + "']]"; } } }
Output is;
<string> { "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"}]} </string>
the result should be as follows:
{ "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"}]}
In the above output i dont want to see the string /string
is there is a way to omit that if so please suggest me.
Thanks in advance
hi
the solution for the above issue is
1. instead ofpublic string GetEmployees()i change it to
public Void GetEmployees()
2.instead of returning the result i used the following code
this.Context.Response.ContentType = "application/json; charset=utf-8"; this.Context.Response.Write(serializer.Serialize(new { Cargo = rows }));
Once i did this now i get the result what i really want:
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"}]}
这篇关于如何忽略Json webservice输出中的默认值(字符串)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!