2013-03-04 84 views
1

我想弄清楚如何創建一個C#類,我可以反序列化這個JSON到。 有人可以指出我正確的方向嗎?如何將此JSON反序列化爲C#對象?

這裏是我的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字符串。

回答

0

ScottGu's blog

ASP.NET MVC 3包括內置的,使 動作方法來接收JSON編碼的數據和模型綁定到 動作方法參數JSON綁定支持。

不是接收參數爲string,你可以嘗試直接綁定請求你的對象(JSON模型綁定):

[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是另外兩個。

+0

反序列化不會接受一個對象,它必須如下。 反序列化(字符串); //這是函數簽名。 – 2013-03-04 04:09:50

+0

當然,它的工作原理,你介意給我顯示其他文檔嗎?我已經添加了幾個示例(包括指向Phil Hacck的博客的鏈接),顯示了json模型綁定。 – Ulises 2013-03-04 15:15:46

+0

增加了一個鏈接到ScottGu的博客 – Ulises 2013-03-04 15:29:18

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

希望有幫助,

丹尼爾。

+0

這導致了一個異常,說明沒有爲MenuItem []定義無參數的構造函數。如果我定義它,我仍然會收到異常。 – 2013-03-04 04:06:07

+0

我剛剛在Mvc 4站點(.Net 4.5)中測試過,它工作正常。你可以在這個預覽中看到它:http://screencast.com/t/slucdqpK5 – Daniel 2013-03-04 14:09:27