C# 如何在来自asmx的JSON响应中忽略空值

C# 如何在来自asmx的JSON响应中忽略空值,c#,jquery,json,asmx,C#,Jquery,Json,Asmx,我有一个简单的asmx返回JSON: [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class my

我有一个简单的asmx返回JSON:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class myWebService: System.Web.Services.WebService
    {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public MyCustomClassObject GetTestData()
    {
        MyCustomClassObject x = new MyCustomClassObject();
        x.PropertyA = "1";
        x.PropertyC = "1";
        return x;
    }
 public class MyCustomClassObject 
    {
        public string PropertyA { get; set; }
        public string PropertyB { get; set; }
        public string PropertyC { get; set; }
        public object PropertyD { get; set; }
    }
 var jqxhr = $.ajax(
                {
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: "/WebServices/myWebService.asmx/GetTestData",
                    data: parameters,
                    dataType: "json",
                    success: successLoadingData,
                    error: errorLoadingData,
                    complete: function () { $("#LoadingImage").hide(); }
                });
c#类定义:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class myWebService: System.Web.Services.WebService
    {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public MyCustomClassObject GetTestData()
    {
        MyCustomClassObject x = new MyCustomClassObject();
        x.PropertyA = "1";
        x.PropertyC = "1";
        return x;
    }
 public class MyCustomClassObject 
    {
        public string PropertyA { get; set; }
        public string PropertyB { get; set; }
        public string PropertyC { get; set; }
        public object PropertyD { get; set; }
    }
 var jqxhr = $.ajax(
                {
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: "/WebServices/myWebService.asmx/GetTestData",
                    data: parameters,
                    dataType: "json",
                    success: successLoadingData,
                    error: errorLoadingData,
                    complete: function () { $("#LoadingImage").hide(); }
                });
使用jquery$.ajax调用:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class myWebService: System.Web.Services.WebService
    {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public MyCustomClassObject GetTestData()
    {
        MyCustomClassObject x = new MyCustomClassObject();
        x.PropertyA = "1";
        x.PropertyC = "1";
        return x;
    }
 public class MyCustomClassObject 
    {
        public string PropertyA { get; set; }
        public string PropertyB { get; set; }
        public string PropertyC { get; set; }
        public object PropertyD { get; set; }
    }
 var jqxhr = $.ajax(
                {
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: "/WebServices/myWebService.asmx/GetTestData",
                    data: parameters,
                    dataType: "json",
                    success: successLoadingData,
                    error: errorLoadingData,
                    complete: function () { $("#LoadingImage").hide(); }
                });
我的JSON响应(带有不需要的空值):

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class myWebService: System.Web.Services.WebService
    {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public MyCustomClassObject GetTestData()
    {
        MyCustomClassObject x = new MyCustomClassObject();
        x.PropertyA = "1";
        x.PropertyC = "1";
        return x;
    }
 public class MyCustomClassObject 
    {
        public string PropertyA { get; set; }
        public string PropertyB { get; set; }
        public string PropertyC { get; set; }
        public object PropertyD { get; set; }
    }
 var jqxhr = $.ajax(
                {
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: "/WebServices/myWebService.asmx/GetTestData",
                    data: parameters,
                    dataType: "json",
                    success: successLoadingData,
                    error: errorLoadingData,
                    complete: function () { $("#LoadingImage").hide(); }
                });
{“PropertyA”:“1”,“PropertyB”:null,“PropertyC”:“1”,“PropertyD”:null}

问题: 如何仅在JSON中使用尽可能多的已有属性来获取非空属性


我在这里看到了一些答案,人们返回JSON对象和用JSON属性定义的属性,但我只是返回我的对象,Web服务正在为我将其转换为JSON(由于Response.Format属性)。如果必须的话,我会改变我的方法,但这是我的第一个JSON项目,所以我希望保持简单。谢谢。

继续评论部分。 即使你调用一个函数来删除空值,我个人的看法是,这是一个糟糕的设计,拥有一个字典和序列化,这是一种比在完成后删除我们不想要的属性更优雅的方式

我会这样做:

public class MyCustomClassObject 
{
    public Dictionary<string, object> Foo { get; set; }

    public MyCustomClassObject()
    {
        this.Foo = new Dictionary<string, object>();
    }

}

public MyCustomClassObject GetTestData()
{
    MyCustomClassObject x = new MyCustomClassObject();
    x.Foo.Add("PropertyA", 2);
    x.Foo.Add("PropertyC", "3");
    return x.Foo;
}
公共类MyCustomClassObject
{
公共字典Foo{get;set;}
公共MyCustomClassObject()
{
this.Foo=新字典();
}
}
公共MyCustomClassObject GetTestData()
{
MyCustomClassObject x=新的MyCustomClassObject();
x、 Foo.Add(“PropertyA”,2);
x、 Foo.添加(“PropertyC”、“3”);
返回x.Foo;
}
这为您提供了一个更通用的对象,并且更好地遵循JSON格式,因为理论上可以将对象列表或数组作为一个值,这也更适合于使用,因为您可以在此处添加PropertyD


为什么您需要在添加值后删除这些值?

您可以递归删除空属性,下面是一个代码段:

function removeNulls(obj){
    var res = {};
    for(var key in obj){
        if (obj[key] !== null && typeof obj[key] == "object")
            res[key] = removeNulls(obj[key]);
        else if(obj[key] !== null)
            res[key] = obj[key];        
    }
    return res;
};
用法是
removeNulls(jsonResult)


在行动中看到它

反问句,但如果一个值为null,它真的应该存在吗?有没有办法通过使用某种列表对象来解决这个问题?为什么要这样做?只写这么多行代码值得吗?@ThomasLindvall:我想我可以换成一个列表,但我希望有一些简单的方法可以忽略空值,比如在json.net中的NullValueHandling.Ignore。也许我应该学习如何使用json.net?@L.B你是在回应我还是托马斯?你能扩展一下吗,你的评论对我没有帮助对不起。谢谢,我会使用Thomas的解决方案,但这是一个有用的片段,我会记住其他场景。