3

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"
       }
   }
3
  • What is the problem you are experiencing ? What is the expected behavior and what are you getting ? Commented Dec 1, 2015 at 16:40
  • are you sure that you init Foo in GetData()? Commented Dec 1, 2015 at 16:41
  • Yes, because I'm using the GetData() in other methods on server, and all properties are filled with data. The problem is the json object. It doesn't have the properties Commented Dec 1, 2015 at 16:48

2 Answers 2

5

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Switched to this, because default serializer, returns null for derived class properties overrided with new keyword.
0

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.

2 Comments

I understand that I need to set value to the property to view it on json response. The Issue I'm facing is that the method GetData() do initialize all the properties of ComplexA including the Complex1 property.. and inside the Foo class: Name and Prop1. All this properties has value
When you put a breakpoint, are you seeing the fully loaded object before calling the Json method in the controller ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.