【问题标题】:VB.net - JSON parse - NewtonsoftVB.net - JSON 解析 - Newtonsoft
【发布时间】:2013-11-13 12:17:27
【问题描述】:

我在使用带有 Newtonsoft Json 库的 vb.net 解析 JSON 时遇到了麻烦。

我的 JSON 数据如下:

{
"Result":"Success",
"UserID":"johns",
"Password":null,
"Locked":"False",
"Comment":"",
"LastLoggedOn":"11/9/2013 9:14:17 PM",
"NumFailedAttempts":"1",
"FirstName":"John",
"LastName":"Smith",
"MessageNum":"UA-000",
"MessageText":"Authorisation successful"
}

我的代码如下:

Dim a As saLoginResponse = JsonConvert.DeserializeObject(Of saLoginResponse)(strJSONEncode)
            Response.Write(a.ToString)

Response.Write(a.MessageText)

这不会产生任何输出。

感谢任何帮助。

【问题讨论】:

    标签: json vb.net json.net


    【解决方案1】:

    假设您的 saLoginResponse 类定义如下,并且您的 strJSONEncode 字符串包含您在问题中发布的 JSON 数据,您的代码应该可以正常工作。

    Public Class saLoginResponse
        Public Property Result As String
        Public Property UserID As String
        Public Property Password As String
        Public Property Locked As Boolean
        Public Property Comment As String
        Public Property LastLoggedOn As String
        Public Property NumFailedAttempts As String
        Public Property FirstName As String
        Public Property LastName As String
        Public Property MessageNum As String
        Public Property MessageText As String
    End Class
    

    演示:

    Sub Main()
    
        Dim json As String = _
        "{" + _
        """Result"":""Success""," + _
        """UserID"":""johns""," + _
        """Password"":null," + _
        """Locked"":""False""," + _
        """Comment"":""""," + _
        """LastLoggedOn"":""11/9/2013 9:14:17 PM""," + _
        """NumFailedAttempts"":""1""," + _
        """FirstName"":""John""," + _
        """LastName"":""Smith""," + _
        """MessageNum"":""UA-000""," + _
        """MessageText"":""Authorisation successful""" + _
        "}"
    
        Dim a As saLoginResponse = JsonConvert.DeserializeObject(Of saLoginResponse)(json)
    
        Debug.WriteLine(a.MessageText + " for " + a.FirstName + " " + a.LastName)
    
    End Sub
    

    调试窗口中的输出:

    Authorisation successful for John Smith
    

    【讨论】:

    • 干杯布赖恩。是的,我的类是按照你提到的方式定义的,我的 json 字符串有数据。我已经验证了 json 数据jsonlint.com。我相信我的代码与您的代码完全相同,但它没有产生输出。 a.* 为空。
    • 如果在新项目中运行上述测试代码会发生什么?这对你有用吗?
    • 不幸的是,没有。我目前正在运行两个代码;一个在客户端,另一个在服务器端。像往常一样,客户端发送用户 ID 和密码,服务器使用上述 json 响应它。两者都是在两台不同的计算机上运行的两个独立项目。两者都不解析任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多