在vb中反序列化JSON数据

时间:2014-01-16 17:51:20

标签: json vb.net serialization

我一直在尝试反序列化JSON数据,这是我在搜索StackOverflow之前已经走了多远,我跟着一篇文章,但它仍然破坏了。我在这一行上遇到了运行时错误:

Dim jsonObject As Newtonsoft.Json.Linq.JArray = JsonConvert.DeserializeObject(json)

声明“无法将对象转换为字符串”。 提前致谢:D

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Structure JSONList
    Dim URL, Name As String 
End Structure

Public Class Form1
    Dim JSONList As List(Of ListViewItem)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim request As WebRequest = WebRequest.Create("http://www.reddit.com/r/EarthPorn.json")
        Dim response As WebResponse = request.GetResponse()

        ' Get the stream containing content returned by the server.
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        MessageBox.Show(responseFromServer)

        Dim json As String = responseFromServer
        Dim ser As JObject = JObject.Parse(json)
        Dim data As List(Of JToken) = ser.Children().ToList
        Dim output As String = ""
        Dim jsonObject As Newtonsoft.Json.Linq.JArray = JsonConvert.DeserializeObject(json)

        Dim JSONDecode() As JSONList = (From j In jsonObject
                                         Select New JSONList() With {.URL = j("URL"),
                                                                     .Name = j("Name")}
                                       ).ToArray()

        MessageBox.Show(JSONDecode(0).Name)

    End Sub
End Class

0 个答案:

没有答案