简体   繁体   English

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

[英]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. 我试图弄清楚如何创建一个C#类,可以将该json反序列化为。 Can somebody point me in the right direction? 有人可以指出我正确的方向吗?

Here is my 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"
        }
    }
}

} }

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. 在我的控制器中,我使用以下代码尝试将json反序列化为我的对象。

 [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. 注意:菜单变量包含JSON字符串。

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. 您当前的JSON中有4个菜单项...我想可能会更改为5或6,对吧?...如果这样...您的JSON不正确,则应使用数组。

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: 然后,您可以将JSON反序列化为“ MenuItems数组” ...,例如:

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

Hope that helps, 希望能有所帮助,

Daniel. 丹尼尔。

From ScottGu's blog : 来自ScottGu的博客

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. ASP.NET MVC 3包含内置的JSON绑定支持,该支持使操作方法可以接收JSON编码的数据并将其模型绑定到操作方法参数。

Instead of receiving the parameter as string you could try binding the request directly to your object ( json model binding ): 除了将参数作为string接收之外,您可以尝试将请求直接绑定到对象( json模型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: 另外,请确保客户端将请求的内容类型作为json发送,例如:

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

See Phil Haack's example 参见Phil Haack的示例

Here and here are two more. 这里这里还有两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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