2014-04-03 66 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