VB.Net代码和JSON类 [英] VB.Net code and JSON class
问题描述
我有这个VB.Net代码和JSON类,当我执行它时,没有在对象中得到字符串.
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":选择","graphtype":"pie",问题":您最喜欢的电影是什么","a1":霍比特人","a2":哈利波特","a3":超人","a4":蝙蝠侠","a5":","a6":","a7":","a8":","img" :","duration":-1","start":"1419459873","end":-1"},"a":[{"id":"98656","time":"1419459873," person:"," person_id:"," response:"超人,"评论:" Perfect!," modlevel:"},{" id:" 98657","time":"1419460159","person":","person_id":","response":"Hobbit","comment":来自我的手机","modlevel":"},{ "id":"98658","time":"1419460281","person":","person_id":","response":"Hobbit","comment":来自另一个手机","modlevel :"},{" id:" 98662," time:" 1419523411," person:"," person_id:"," response:" Batman," comment:"Gaz!昨天感谢您收到超级肯定的电子邮件!," modlevel:"}]],"成功:true}
{"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:
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)
输出:
Q:您最喜欢的电影是A1:超人成功:是
Q: What's your favorite movie, A1:Superman success: True
正如您所发现的,End
是VB中的关键字,因此在您的A
类中这是非法的,因此您必须使用其他名称.但是,这意味着除非使用<JsonProperty>
属性,否则不会对该字段进行反序列化.但是除了更改名称之外,您不必将其与其他名称一起使用.
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.
这篇关于VB.Net代码和JSON类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!