VB.net - JSON解析 - Newtonsoft

时间:2013-11-13 12:17:28

标签: json vb.net json.net

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

这不会产生任何输出。

感谢任何帮助。

1 个答案:

答案 0 :(得分: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