0

我在使用带有 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)

这不会产生任何输出。

任何帮助表示赞赏。

4

1 回答 1

0

假设您的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
于 2013-11-13T15:26:17.867 回答