如何将此JSON反序列化为C#对象?

时间:2013-03-04 03:16:05

标签: c# javascript json asp.net-mvc-4

我正在试图弄清楚如何创建一个C#类,我可以将这个json反序列化为。 有人能指出我正确的方向吗?

这是我的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"
        }
    }
}

}

我试过以下,没有运气

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; }   
}

在我的控制器中,我正在使用以下内容尝试将json反序列化为我的对象。

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

注意:菜单变量包含JSON字符串。

2 个答案:

答案 0 :(得分:1)

您当前的JSON中有4个菜单项...我猜这可能会改为5或6,对吧?......如果是这样......你的JSON不正确,你应该使用数组。

类似的东西:

[
{
    "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"
        }
    ]
}
]

然后定义你的课程:

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;
    }
}

然后你可以将你的JSON反序列化为“MenuItems数组”......如:

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

希望有所帮助,

丹尼尔。

答案 1 :(得分:0)

来自ScottGu's blog

  

ASP.NET MVC 3包含支持的内置JSON绑定支持   用于接收JSON编码数据并将其模型绑定到的动作方法   动作方法参数。

您可以尝试将请求直接绑定到您的对象( json模型绑定),而不是将参数作为string接收:

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

此外,请确保客户端将请求的内容类型发送为json,例如:

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

See Phil Haack's example

Herehere还有两个。