【问题标题】:how to trim the white spaces in the output of json webservice asp.net如何修剪json webservice asp.net输出中的空格
【发布时间】:2014-04-04 12:19:14
【问题描述】:

嗨(我是 JSON 新手,这是我的第一个网络服务)

我在 asp.net 中创建了一个返回 JSON 数据的 Web 服务。一切正常,但我需要的是从数据库中获取数据时,它应该在显示之前修剪空白。

因为在我的数据库中,我将字段类型指定为 nvarchar(100),但插入了

{"Cargo":[{"Id":1,"FirstName":"devi                ","LastName":"priya               ","Contactno":"965577796 "}]}

在名称 devi 和 priya 之后有很多空格。 但我应该得到的实际结果是;

{"Cargo":[{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "}]}

这是我的代码;

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, XmlSerializeString = false)]
        //public string GetEmployees(string SearchTerm) 

        public void GetEmployees()
        {
            try
            {
                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 serializer.Serialize(rows);
                //return "{ \"Cargo\": " + serializer.Serialize(rows) + "}";
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //string strJSON = js.Serialize(new { Cargo = rows });
                this.Context.Response.ContentType = "application/json; charset=utf-8";
                this.Context.Response.Write(serializer.Serialize(new { Cargo = rows }));
                //return serializer.Serialize(new { Cargo = rows });

            }
            catch (Exception ex)
            {
                //return errmsg(ex);
            }
        }
} 

谁能帮帮我。

提前致谢。

【问题讨论】:

  • 嗨,下面的代码可以帮助我修剪空格:row.Add(col.ColumnName, rs[col].ToString().Trim());

标签: asp.net json web-services


【解决方案1】:

您好,以下代码可以帮助我修剪空白:

row.Add(col.ColumnName, rs[col].ToString().Trim()); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2017-09-16
    相关资源
    最近更新 更多