如何在json webservice asp.net的输出中修剪空白

Appdev

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

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

因为在我的数据库中,我已将字段类型指定为nvarchar(100),但插入了<20个字符的文本,所以在输出中,我也获得了带空格的结果,例如:

{"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);
            }
        }
} 

谁能帮帮我吗。

提前致谢。

Appdev

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在jquery中检索json数据。asp.net webService C#

如何在Jersey Rest Webservice中接受json数组输入

如何在Restful WebService Java中读取JSON负载

Webservice模板:如何在webservice模板中传递标题

如何在 webService Rest 中获取 arrayJSON?

如何使 ASP .net Webservice 函数参数成为非强制参数

在ASP.NET中阻止对WebService.asmx的访问

如何在字符串下的JSON中响应?使用PHP Webservice

在 asp.net 中使用 Ajax Jquery 调用 webservice

回传我的C#asp.net WebService的URL

WebService的Bootstrap Typeahead ASP.NET(Webforms)数据

如何在ASP.NET Web API中接收JSON?

如何从webservice获取json

如何在Webservice Junit测试中忽略验证/ ConstraintValidator?

如何在springboot中设置webservice响应的媒体类型?

Webservice-如何在datetime元素中传递时区信息

如何在 C# Webservice 中做新行

如何在浏览器中打开python webservice

如何在Nlog中JsonPost到WebService目标

如何在webservice中更新我的记录值sql

如何在Rest WebService中获取请求标头值

如何从wsdl Webservice的xml输出中获取ID

在php webservice中解析json请求

在从JavaScript到ASP.NET WebService的调用过程中,如何将“ NULL”值传递给可为空的字段

如何访问 Webservice 文件中的 Gridview

如何在Go模板中修剪空白

无法在 javascript 中附加从 asp.net webservice 返回的数组值

从restful webservice下载图像并在razor中显示使用asp.net MVC

如何在文件asp.net中下载json对象