【问题标题】:vb.net receive json data in POST where key has forward slashvb.net 在 POST 中接收 json 数据,其中键有正斜杠
【发布时间】:2018-05-22 17:23:33
【问题描述】:

我有一个安静的网络服务,它正在接收一个带有 json 数据的 POST。这是 json 示例,其中第三个密钥/对在密钥名称中带有正斜杠。

{
    "_notes": "Test",
    "_received": true,
    "item/id": "8a69d38fba4c40d5a3d730807db87859"
}

这是我的 Post 方法

Public Sub Post(value As Testing)

这里是测试类的定义

Public Class Testing
    Public _notes As String
    Public _received As Boolean
    Public item/ID As String
End Class

我得到一个编译器错误,因为我不能在变量名中使用正斜杠。我应该以不同的方式捕获我身边的数据吗?不幸的是,我无法控制 json 中的键名。

【问题讨论】:

    标签: vb.net rest http-post


    【解决方案1】:

    假设您使用的是 .NET Web API 框架的内置反序列化功能,您应该花一些时间了解这些序列化程序以及如何控制它们。 Here is a good introductory point 在文档中。

    默认情况下,内置的东西使用 JSON.NET 进行 JSON 序列化,它有 a number of attributes 允许你控制它。您将对这个问题感兴趣的是JsonPropertyAttribute。例如:

    Public Class Testing
        Public _notes As String
        Public _received As Boolean
    
        <JsonProperty("item/id")>
        Public ItemID As String
    End Class
    

    【讨论】:

    • 谢谢!我一直在努力寻找文档,因为一切似乎都是关于制作 POST 而不是处理它。
    猜你喜欢
    • 2018-01-10
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多