VB.Net 代码和 JSON class

VB.Net code and JSON class

我有这个 VB.Net 代码和 JSON class,当我执行它时,我没有得到对象中的字符串。

{"q":{"type":"choice","graphtype":"pie","question":"What's your favorite movie","a1":"Hobbit","a2":"Harry Potter","a3":"Superman","a4":"Batman","a5":"","a6":"","a7":"","a8":"","img":"","duration":"-1","start":"1419459873","end":"-1"},"a":[{"id":"98656","time":"1419459873","person":"","person_id":"","response":"Superman","comment":"Perfect!","modlevel":""},{"id":"98657","time":"1419460159","person":"","person_id":"","response":"Hobbit","comment":"from my mobile","modlevel":""},{"id":"98658","time":"1419460281","person":"","person_id":"","response":"Hobbit","comment":"from another mobile","modlevel":""},{"id":"98662","time":"1419523411","person":"","person_id":"","response":"Batman","comment":"Hi Gaz! Thanks for the super positive email yesterday!","modlevel":""}],"success":true}

Imports System
Imports System.IO
Imports System.Net
Imports System.Collections.Generic
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim PollCode As String = "xxxxxxx"
    Dim req As HttpWebRequest = HttpWebRequest.Create(PollCode)
    Dim re As HttpWebResponse = req.GetResponse()
    Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
    Dim a As Q = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Q)(src)
    MsgBox(a.Question)
    'GridView1.DataSource = Newtonsoft.Json.JsonConvert.DeserializeObject(src)
End Sub

End Class


Public Class Q

<JsonProperty("type")>
Public Property Type As String

<JsonProperty("graphtype")>
Public Property Graphtype As String

<JsonProperty("question")>
Public Property Question As String

<JsonProperty("a1")>
Public Property A1 As String

<JsonProperty("a2")>
Public Property A2 As String

<JsonProperty("a3")>
Public Property A3 As String

<JsonProperty("a4")>
Public Property A4 As String

<JsonProperty("a5")>
Public Property A5 As String

<JsonProperty("a6")>
Public Property A6 As String

<JsonProperty("a7")>
Public Property A7 As String

<JsonProperty("a8")>
Public Property A8 As String

<JsonProperty("img")>
Public Property Img As String

<JsonProperty("duration")>
Public Property Duration As String

<JsonProperty("start")>
Public Property Start As String

<JsonProperty("end")>
Public Property EndMe As String
End Class

Public Class A

<JsonProperty("id")>
Public Property Id As String

<JsonProperty("time")>
Public Property Time As String

<JsonProperty("person")>
Public Property Person As String

<JsonProperty("person_id")>
Public Property PersonId As String

<JsonProperty("response")>
Public Property Response As String

<JsonProperty("comment")>
Public Property Comment As String

<JsonProperty("modlevel")>
Public Property Modlevel As String
End Class

Public Class SampleClass

<JsonProperty("q")>
Public Property Q As Q

<JsonProperty("a")>
Public Property A As A()

<JsonProperty("success")>
Public Property Success As Boolean
End Class

SampleClass 是已经序列化的,包含1个Q和几个A对象(在本例中)。您正在尝试仅反序列化 Q ,这是行不通的。我打电话给 SampleClass RootQA:

Dim myQA As RootQA
myQA = JsonConvert.DeserializeObject(Of RootQA)(jstr)
' print Q, A(0) and success from the root object
Console.WriteLine("Q: {0}, A1:{1} success: {2}", myQA.q.question, myQA.a(0).response, myQA.success)

输出:

Q: What's your favorite movie, A1:Superman success: True

如您所见,End 是 VB 中的关键字,因此在您的 A class 中是非法的,因此您必须使用不同的名称。但是,这意味着该字段不会被反序列化,除非您使用 <JsonProperty> 属性。但是你不必将它与其他人一起使用,除非你正在更改名称。