【问题标题】:WebAPI Converting JSON in to Strongly typed .net ObjectWebAPI 将 JSON 转换为强类型的 .net 对象
【发布时间】:2015-06-11 10:05:04
【问题描述】:

我想要做的是将一些 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)

【问题讨论】:

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


    【解决方案1】:

    要了解您可以使用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; }
    }
    

    【讨论】:

    • 非常感谢!这是工具箱的一个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2014-03-07
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多