Pure.Krome
Pure.Krome

Reputation: 87087

How to format some ASP.NET MVC Json result?

i've got a really simple POCO (business) object which I'm returning 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. Now, this object includes following public properties...

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

Now when the object is serialized into json, the decimal score has a precision of 7 (and i'm after a precision of 2) and the KeyValues might be null. If it's null, the the json looks like this...

"KeyValues" : null

I was hoping to have the KeyValues NOT be included in the json, if it's null.

Are there any tricks to helping format this json output? Or do i need to manually do this .. make my own string .. then return it as .. i donno .. a ContentAction? (eeks).

please help!

Upvotes: 2

Views: 2152

Answers (1)

womp
womp

Reputation: 116987

The ASP.Net MVC Json() method uses the JavascriptSerializer internally to do it's encoding. There are some options to control the serialization of your classes by creating and registering your own JavascriptConverter objects.

Upvotes: 1

Related Questions