How do I format some ASP.NET MVC Json result?

I have a really simple POCO (business) object that I return to the client as some json using ASP.NET MVC.

eg. (please ignore the lack of error checking, etc.).

public JsonAction Index()
{
    Foo myFoo = MyService();
    return Json(myFoo);
}

      

Kewl. This object now includes the following public properties ...

public class Foo
{
    public decimal Score { get; set; }
    public Dictionary<string, string> KeyValues { get; set; }
}

      

Now when the object is serialized to json, the decimal value has precision 7 (and I after precision 2) and KeyValues ​​can be null. If it is zero the json looks like this ...

"KeyValues" : null

      

I was hoping that KeyValues ​​would NOT be included in the json if invalid.

Are there any tricks to help format this json output? Or do I have to do it manually .. make my own string ... then return it like .. I don't know .. ContentAction? (EEKs).

Please, help!

+2


source to share


1 answer


ASP.Net MVC Json () method uses JavascriptSerializer for internal encoding. There are several options for controlling the serialization of your classes by creating and registering your own JavascriptConverter objects .



+1


source







All Articles