简体   繁体   中英

How can I deserialize this JSON to a C# object?

I'm trying to figure out how to create a C# class that I can deserialize this json into. Can somebody point me in the right direction?

Here is my json

{
"0": {
    "heading": "Home",
    "link": "#",
    "dropdown": {}
},
"1": {
    "heading": "About",
    "link": "#",
    "dropdown": {
        "0": {
            "name": "Programs",
            "value": "programs"
        },
        "1": {
            "name": "Sample Page",
            "value": "test"
        },
        "2": {
            "name": "Donations",
            "value": "donations"
        }
    }
},
"2": {
    "heading": "Products",
    "link": "#",
    "dropdown": {}
},
"3": {
    "heading": "Contact Us",
    "link": "#",
    "dropdown": {
        "0": {
            "name": "Programs",
            "value": "programs"
        },
        "1": {
            "name": "Donations",
            "value": "donations"
        }
    }
}

}

I've tried the following, with no luck

public class Menu
{
    public MenuItem MenuItems { get; set; }
}

public class MenuItem
{
    public string Heading { get; set; }
    public string Link { get; set; }
    public DropDownMenu DropDownMenu { get; set; }
}

public class DropDownMenu
{
    public string Name { get; set; }
    public string Value { get; set; }   
}

In my controller I'm using the following to try and deserialize the json into my object.

 [HttpPost]
 public ActionResult AddMenu(string menuType, string menu, string menuTitle)
 {
     var serializer = new JavaScriptSerializer();
     var newMenu = serializer.Deserialize<Menu>(menu);
  }

Note: The menu variable contains the JSON string.

Your current JSON has 4 menu items in it... I am guessing that could change to 5 or 6, right?... if so... your JSON is incorrect, you should use an array.

Something like:

[
{
    "heading": "Home",
    "link": "#",
    "dropdown": []
},
{
    "heading": "About",
    "link": "#",
    "dropdown": [
        {
            "name": "Programs",
            "value": "programs"
        },
        {
            "name": "Sample Page",
            "value": "test"
        },
        {
            "name": "Donations",
            "value": "donations"
        }
    ]
},
{
    "heading": "Products",
    "link": "#",
    "dropdown": []
},
{
    "heading": "Contact Us",
    "link": "#",
    "dropdown": [
        {
            "name": "Programs",
            "value": "programs"
        },
        {
            "name": "Donations",
            "value": "donations"
        }
    ]
}
]

And then define your class:

public class MenuItem
{
    public string heading
    {
        get;
        set;
    }

    public string link
    {
        get;
        set;
    }

    public DropDownMenu[] dropdown
    {
        get;
        set;
    }
}

public class DropDownMenu
{
    public string Name
    {
        get;
        set;
    }
    public string Value
    {
        get;
        set;
    }
}

Then you can deserialize your JSON as an "Array of MenuItems"... like:

var ser = new JavaScriptSerializer();
var newMenu = ser.Deserialize<MenuItem[]>(json);

Hope that helps,

Daniel.

From ScottGu's blog :

ASP.NET MVC 3 includes built-in JSON binding support that enables action methods to receive JSON-encoded data and model-bind it to action method parameters.

Instead of receiving the parameter as string you could try binding the request directly to your object ( json model binding ):

[HttpPost]
 public ActionResult AddMenu(string menuType, Menu menu, string menuTitle)
 {
   // use menu here, no need to deserialize anything else
  }

Also, make sure the client sends the request's content type as json, for example:

contentType: 'application/json; charset=utf-8',

See Phil Haack's example

Here and here are two more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM