2014-04-03 34 views
1

我需要忽略执行webservice JSON时显示的输出中的默认值(* string *)。如何忽略Json webservice输出中的默认值(字符串)

这里是我的代码:

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 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 + "']]"; 
     } 
    } 
} 

输出的;

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

但我需要的实际输出是;

{ "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"}]} 

在上面的输出我不希望看到的串/串 是有办法忽略,如果这样,请给我建议。

在此先感谢

回答

0

尝试用return serializer.Serialize(new { Cargo = rows });

+0

我试过这个,但仍然显示字符串和/字符串。 – Appdev

+0

您是否使用jQuery或JavaScript函数来使用此服务?如果是这样,你是否可以在你的问题中添加该代码。 – viki