我正在尝试处理下面的JSON输入。代码(底部)抛出
Error reading JObject from JsonReader.
Current JsonReader item is not an object: StartArray.
Path '', line 1, position 1.
输入:
[
{
"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
}
],
}
]
}
}
}
代码:
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
我使用
访问此命名空间Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
我做错了什么?
答案 0 :(得分:0)
我使用JSONLint来调试您的JSON,因为它无效。你必须检查你的消息来源是否发送了无效的JSON,或者在这里发布时有错字。这将是有效的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
}]
}]
}
}]
}]
然后,您可以使用JSON Utils或VS本身等工具(将JSON复制到剪贴板并使用编辑>选择性粘贴>将JSON粘贴为类)来生成VB类与JSON结构匹配:
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
无论如何,如果你将上面的类添加到项目中,你可以这样做:
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)
使用结构化对象。我觉得值得一试。我希望它可以帮助你。