WebAPI将JSON转换为强类型.net对象

时间:2015-06-11 10:05:04

标签: c# asp.net json asp.net-web-api

我尝试做的是将一些JSON发送到.net网络服务器,JSON看起来像这样。

            var mydata =
                {
                    "filters": {
                        "game": -1,
                        "mediatype": -1,
                        "location": -1,
                        "contributor": -1
                    },
                    "tags": [1,2,3,4],
                    "search": "",
                    "startindex": 6,
                    "fetchcount": 12
                }

根据我一直在阅读的内容,Web API会自动将此JSON转换为强类型的.net对象。 我仍然清楚地知道这些物体的结构是如何工作的。任何人都可以告诉我.net对象应该是什么样的,以便这个JSON对象自动转换。

我打算在我的控制器中做这样的事情

public int FilterLoad([FromBody]mydata> test)

1 个答案:

答案 0 :(得分:2)

要了解您可以使用http://json2csharp.com/

对于这个json,它产生了这个:

public class Filters
{
    public int game { get; set; }
    public int mediatype { get; set; }
    public int location { get; set; }
    public int contributor { get; set; }
}

public class RootObject
{
    public Filters filters { get; set; }
    public List<int> tags { get; set; }
    public string search { get; set; }
    public int startindex { get; set; }
    public int fetchcount { get; set; }
}