本文介绍了VB.Net代码和JSON类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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.

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)

输出:

正如您所发现的,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类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:05