簡體   English   中英

如何將該JSON反序列化為C#對象?

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

我試圖弄清楚如何創建一個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字符串。

您當前的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);

希望能有所幫助,

丹尼爾。

來自ScottGu的博客

ASP.NET MVC 3包含內置的JSON綁定支持,該支持使操作方法可以接收JSON編碼的數據並將其模型綁定到操作方法參數。

除了將參數作為string接收之外,您可以嘗試將請求直接綁定到對象( json模型binding ):

[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',

參見Phil Haack的示例

這里這里還有兩個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM