Lemon Knife Lemon Knife - 5 months ago 85
JSON Question

VB.NET Json Parse


[
{
"data": [
{
"photos": {
"data": [
{
"id": "1",
"name": "test Post",
"images": [
{
"height": 1024,
"source": "https://127.0.0.1:8080/image1.png",
"width": 1204
},
{
"height": 1204,
"source": "https://127.0.0.1:8080/image2.png",
"width": 1204
}
],
}
]
}
}
}



I received this Json String.
im write this code error.

Dim ser As JObject = JObject.Parse(result.ToString)
Dim data As List(Of JToken) = ser.Children().ToList

Dim jsonString As String = Nothing
Dim index As Integer = 0
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "data"
jsonString = item.Value.ToString
End Select
Next

ser = JObject.Parse(jsonString)
data = ser.Children().ToList
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "photos"
jsonString = item.Value.ToString
End Select
Next

ser = JObject.Parse(jsonString)
data = ser.Children().ToList
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "data"
For Each comment As JObject In item.Values
Dim _id As String = comment("id")
Dim _title As String = comment("title")
Dim _name As String = comment("name")
Dim _source As String = comment("source")
Dim source As List(Of JToken) = item.Value.ToList
For Each images As JProperty In source
item.CreateReader()
Select Case item.Name
Case "images"
Dim _tempsource As String = source("source")
_source += _tempsource & vbCrLf
End Select
Next
Dim _created_time As String = comment("created_time")

Dim itm As New ListViewItem(_id, 1)
itm.SubItems.Add(_title)
itm.SubItems.Add(_name)
itm.SubItems.Add(_source)
itm.SubItems.Add(_created_time)

lvItems.Items.AddRange(New ListViewItem() {itm})
index += 1
Next
End Select
Next


Please suggest a better way.

i worked this namespace with

Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq

Answer

I used JSONLint to debug your JSON, because it is not valid. you must check whether your source is sending you invalid JSON, or there was a typo when posting it here. This would be valid JSON:

[{
    "data": [{
        "photos": {
            "data": [{
                "id": "1",
                "name": "test Post",
                "images": [{
                    "height": 1024,
                    "source": "https://127.0.0.1:8080/image1.png",
                    "width": 1204
                }, {
                    "height": 1204,
                    "source": "https://127.0.0.1:8080/image2.png",
                    "width": 1204
                }]
            }]
        }
    }]
}]

Then, You can use a tool like JSON Utils or VS itself (copying JSON to the clipboard and using Edit > Paste Special > Paste JSON as Classes) to generate VB classes that match JSON structure:

Public Class Image
    Public Property height As Integer
    Public Property source As String
    Public Property width As Integer
End Class

Public Class PhotoDatum
    Public Property id As String
    Public Property name As String
    Public Property images As Image()
End Class

Public Class Photos
    Public Property data As PhotoDatum()
End Class

Public Class Datum
    Public Property photos As Photos
End Class

Public Class SER
    Public Property data As Datum()
End Class

Anyway, if you add the classes above to your project, you can just do this:

Dim MySER = JsonConvert.DeserializeObject(Of SER)(jsonString)
Dim itm As ListViewItem
Dim MyItems As New List(Of ListViewItem)
For Each d In MySER
    If d.photos IsNot Nothing Then
        For each dd in d.photos.data
            If dd.images IsNot Nothing Then
                For each img In dd.images
                    itm = New ListViewItem(dd.id, 1)
                    itm.SubItems.Add(dd.name)
                    itm.SubItems.Add(img.source)
                    MyItems.Add(itm)
                Next
            End If 
        Next
    End If
Next
lvItems.Items.AddRange(MyItems)

And work with your structured objects. I think it is worth a try. I hope it helps you.

Comments