VB.net - JSON 解析 - Newtonsoft

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19953604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:48:04  来源:igfitidea点击:

VB.net - JSON parse - Newtonsoft

jsonvb.netjson.net

提问by Sukhi

I am having a troble parsing JSON using vb.net with Newtonsoft Json library.

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

My JSON Data is as follows :

我的 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"
}

My code is as follows :

我的代码如下:

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

Response.Write(a.MessageText)

This does not produce any output.

这不会产生任何输出。

Any help is appreciated.

任何帮助表示赞赏。

回答by Brian Rogers

Assuming your saLoginResponseclass is defined like the following, and your strJSONEncodestring contains the JSON data you posted in your question, your code should work fine.

假设您的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

Demo:

演示:

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

Output in debug window:

调试窗口中的输出:

Authorisation successful for John Smith