【问题标题】:Simple working Example of json.net in VB.netVB.net 中 json.net 的简单工作示例
【发布时间】:2014-02-10 12:02:36
【问题描述】:

我有以下来自提供商的简化 JSON 字符串,我已经很久没有使用 Visual Studio 和 vb.Net,所以我很生疏!

{
"Venue": {
    "ID": 3145,
    "Name": "Big Venue, Clapton",
    "NameWithTown": "Big Venue, Clapton, London",
    "NameWithDestination": "Big Venue, Clapton, London",
    "ListingType": "A",
    "Address": {
        "Address1": "Clapton Raod",
        "Address2": "",
        "Town": "Clapton",
        "County": "Greater London",
        "Postcode": "PO1 1ST",
        "Country": "United Kingdom",
        "Region": "Europe"
    },
    "ResponseStatus": {
        "ErrorCode": "200",
        "Message": "OK"
    }
}
}

我想使用 JSON.Net 将其转换为我可以使用的东西,我已经阅读了示例等,JSON.net 看起来像是答案,但我无处可去。

我的 .Net 代码(Me.TextBox1.Text 包含上面显示的 JSON)

Imports Newtonsoft.Json

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim obj As JSON_result
    obj = JsonConvert.DeserializeObject(Of JSON_result)(Me.TextBox1.Text)

    MsgBox(obj.ID)
End Sub
End Class

Public Class JSON_result
    Public ID As Integer
    Public Name As String
    Public NameWithTown As String
    Public NameWithDestination As String
    Public ListingType As String
 End Class

谁能解释一下为什么 obj.ID 总是以 0 结尾,为什么我的类的其他属性都没有填充,我需要做些什么来解决这个问题,没有报告错误。

【问题讨论】:

    标签: json vb.net serialization json.net


    【解决方案1】:

    您的课程 JSON_result 与您的 JSON 字符串不匹配。请注意 JSON_result 将要表示的对象如何包装在另一个名为 "Venue" 的属性中。

    所以要么为此创建一个类,例如:

    Public Class Container
        Public Venue As JSON_result
    End Class
    
    Public Class JSON_result
        Public ID As Integer
        Public Name As String
        Public NameWithTown As String
        Public NameWithDestination As String
        Public ListingType As String
    End Class
    
    Dim obj = JsonConvert.DeserializeObject(Of Container)(...your_json...)
    

    或将您的 JSON 字符串更改为

    {
        "ID": 3145,
        "Name": "Big Venue, Clapton",
        "NameWithTown": "Big Venue, Clapton, London",
        "NameWithDestination": "Big Venue, Clapton, London",
        "ListingType": "A",
        "Address": {
            "Address1": "Clapton Raod",
            "Address2": "",
            "Town": "Clapton",
            "County": "Greater London",
            "Postcode": "PO1 1ST",
            "Country": "United Kingdom",
            "Region": "Europe"
        },
        "ResponseStatus": {
            "ErrorCode": "200",
            "Message": "OK"
        }
    }
    

    或使用例如ContractResolver 解析 JSON 字符串。

    【讨论】:

    • 我使用这个网站jsonutils.com 为 JSON 字符串自动创建类以加快速度。以为我会为其他人提及它。
    • 在一些 ASP.Net 工具包中还有一个来自 Microsoft IIRC 的 VS 插件,但我忘记了名字...
    • @mikro,谢谢你的链接。这将非常方便。谢谢!
    【解决方案2】:
    Imports Newtonsoft.Json.Linq
    
    Dim json As JObject = JObject.Parse(Me.TextBox1.Text)
    MsgBox(json.SelectToken("Venue").SelectToken("ID"))
    

    【讨论】:

      【解决方案3】:

      代替使用这个

      MsgBox(json.SelectToken("Venue").SelectToken("ID"))
      

      你也可以使用

      MsgBox(json.SelectToken("Venue.ID"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 2014-07-12
        • 2021-03-16
        • 2011-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-03-05
        相关资源
        最近更新 更多