I've got as simple asmx returning 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;
    }

c# class definition:

 public class MyCustomClassObject 
    {
        public string PropertyA { get; set; }
        public string PropertyB { get; set; }
        public string PropertyC { get; set; }
        public object PropertyD { get; set; }
    }

Called using jquery $.ajax:

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

My JSON Response (with unwanted null values):

{"PropertyA":"1","PropertyB":null,"PropertyC":"1","PropertyD":null}

Question: How do I only get the non null properties only in JSON using as much of what I have already as possible?

I've seen some answers on here where people are returning JSON objects and properties defined with JSON attributes but I'm simply returning my object and the webservice is converting it to JSON for me (due to the Response.Format attribute). If i have to I will change my approach but its my first JSON project so was hoping to keep it simple. Thanks.

有帮助吗?

解决方案

Continuation from the comment section. Even if you call a function to remove the null values, my personal opinion about it would be that it's bad design, having a dictionary and serializing that is a more elegant way than having to remove the properties we don't want after we're done.

What I would do is something like this:

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

this gives you a more generic object to work with and follows the JSON format better, since you theoretically could have a list or array of objects as a value, this is also more adaptable to work with since you can add the PropertyD here.

Why do you need something that removes the values after you've added them?

其他提示

You can recursively remove the properties that are null, here's a snippet that does that:

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

The usage is removeNulls(jsonResult)

See it in action here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top