ASP MVC 4 returning json complex object

I have a class like this:

[serializable]
public class ComplexA{
   public string A{get;set;}
   public string B{get;set;}
   public Foo Complex1 {get;set;}
}
[Serializable]
public class Foo{
   public string Name{get;set;}
   public Bar Prop1{get;set;}
}
[Serializable]
public class Bar{
   public string A{get;set;}
   public bool B{get;set;}
}

When creating the object of ComplexA and return it to the view (using an ajax call from jQuery), as a result I get somthing like this:

{
   "A":"....",
   "B":"...."
}

I am obtaining the object from a BusinessLayaer, and inspecting it from debug, all the properties has value. So I don't understand why not serializing the entire object.

My Action is like this:

[HttpPost]
public ActionResult GetData(){
    var logic = new BL_Something();
    ComplexA info = logic.GetData();
    return Json(info);
}

So the result I am expecting is this:

{
   "A":"...",
   "B":"...",
   "Complex1":{
       "Name":"...",
       "Prop1": {
          "A":"....",
          "B":"false"
       }
   }

Answers


By default, MVC uses Microsoft's JavascriptSerializer class. I have many times found this class coming up short compared to Json.NET. WebAPI and future versions of MVC have switched to Json.NET as the default serializer.

There are ways to replace the default serializer under the hood, but it's a lot of effort.

You can use Json.NET to serialize your object and return it as json by changing your return like so:

return Content(JsonConvert.SerializeObject(info), "application/json");

This should be fairly easy to test. I believe it will resolve your serialization issues.

For convenience you can put an action method on a controller base class like this:

public virtual ActionResult JsonResponse(object obj)
{
    return Content(JsonConvert.SerializeObject(obj), "application/json");
}

Usage in a controller:

public ActionResult GetData()
{
    var info = new ComplexA
    {
        A = "test",
        B = "BB",
        Complex1 = new Foo {Name = "Ss", Prop1 = new Bar {A = "Name", B = false}}
    };
    return JsonResponse(info);
}

Json method should properly convert your complex object(s).

The below code with hardcoded values should work fine.

[HttpPost]
public ActionResult GetData()
{
    var info = new ComplexA
    {
        A = "test",
        B = "BB",
        Complex1 = new Foo {Name = "Ss", Prop1 = new Bar {A = "Name", B = false}}
    };
    return Json(info);
}

This will generate the below JSON

{
    "A": "test",
    "B": "BB",
    "Complex1": {
        "Name": "Ss",
        "Prop1": {
            "A": "Name",
            "B": false
        }
    }
}

So if you are not getting your Complex1 property, That means, in the GetData method, you are not properly initializing / setting that property value(s). Use visual studio breakpoints and see what it is doing inside your method.


Need Your Help

Web project doesn't run on server if a dependent project is opened in eclipse

java eclipse maven tomcat

I'm developing a web application with Eclipse 4.4.2, Maven and Tomcat v7.0. The application consists of a

IE6 the last three characters in a div are being repeated else where in the page? really weird

html css internet-explorer-6 character repeat

Hey, Basically i have an issues (in IE6) where the last three characters of a line of text in a div are being repeated further down the page even though they are only in the HTML once.