如何修剪json webservice asp.net输出中的空格 [英] how to trim the white spaces in the output of json webservice asp.net

查看:71
本文介绍了如何修剪json webservice asp.net输出中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我在asp.net中创建了一个返回JSON数据的Web服务。每个瘦的gwork都很好但是我需要的是从数据库中获取数据时它应该在显示之前修剪空白区域。



因为在我的数据库中我将​​字段类型指定为nvarchar (100)但是插入了带有< 20个字符的文本,所以在输出中我得到了whitespaves的结果



例如:

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





名称devi和priya之后有很多空格。



但是我应该得到的实际结果是;



 {货物:[{Id:1,FirstName:devi ,LastName:priya,Contactno:965577796}]} 





这是我的代码;



使用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,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);
}
//返回serializer.Serialize(rows);
//返回{\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]); 





最后它应该像



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


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

这会有效吗?


HiII'm new to JSON this is my 1st webservice)

I have created an Webservice in asp.net that returns JSON data.Every thin gworkis fine but what i need is when fetching the data from the database it should trim the white spaces before it displays.

Because in my data base i have given the field type as nvarchar(100) but inserted the text with <20 characters so in the output i'm gettin the result with whitespaves also

For eg:

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



after the name devi and priya there is lots if spaces.

But the actual result i supposed to get is;

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



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, 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);
            }
        }
} 



can any one please help me.

thanks in advance.

解决方案

You need to add

.Trim() 

in the row

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



Finally it should be like

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


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

will this work?


这篇关于如何修剪json webservice asp.net输出中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆