Reputation: 13
I have this VB.Net code and JSON class and when I execute it I don't get the string in the object.
{"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
Upvotes: 1
Views: 1101
Reputation: 38875
SampleClass is what has been serialized, which contains 1 Q
and several A
objects (in this sample). You are trying to deserialize only Q
which wont work. I called 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)
Output:
Q: What's your favorite movie, A1:Superman success: True
As you discovered, End
is a keyword in VB, so that was illegal in your A
class, so you have to use a different name. However, that means that field will not be deserialized unless you use the <JsonProperty>
attribute. But you did not have to use it with the others except where you were changing the name.
Upvotes: 1