2017-07-29 67 views
0

我需要从我从Youtube获得的回应中获得“标题”。 我得到以下错误在第8行,第12位,即,在线路:不能Json反序列化与VB.net的Youtube数据

"items": [ 

刚过 “[”

我得到的错误是:

Exception: Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll ("Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'categoryid.Item' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. 
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 
Path 'items', line 8, position 12."). Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll ("Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'categoryid.Item' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. 
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 
Path 'items', line 8, position 12.") 2.61s  [12096] <No Name> 

我有这样的代码:

Dim m As IEnumerable(Of Rootobject) = JsonConvert.DeserializeObject(Of IEnumerable(Of Rootobject))(res) 

与此JSON数据:

{ 
"kind":"youtube#videoListResponse", 
"etag":"\"m2yskBQFythfE4irbTIeOgYYfBU/jznkhy3_Aws9VtommTkcdOYnAAk\"", 
"pageInfo":{ 
    "totalResults":1, 
    "resultsPerPage":1 
}, 
"items":[ 
    { 
    "kind":"youtube#video", 
    "etag":"\"m2yskBQFythfE4irbTIeOgYYfBU/M_wQmC4lQBaHJGxo79N7WlmqNr8\"", 
    "id":"SSbBvKaM6sk", 
    "snippet":{ 
    "publishedAt":"2009-04-15T20:31:11.000Z", 
    "channelId":"UC2kTZB_yeYgdAg4wP2tEryA", 
    "title":"Blur - Song 2", 
    "description":"Blur 21 -- Celebrating 21 years of Blur. To find out more, click here:http://smarturl.it/blur21y\n\n#blur21\n   \nFollow Blur on Twitter:www.twitter.com/blurofficial \nFind Blur on Facebook:www.facebook.com/blur\n\nMusic video by Blur performing Song 2.", 
    "thumbnails":{ 
    "default":{ 
     "url":"https://i.ytimg.com/vi/SSbBvKaM6sk/default.jpg", 
     "width":120, 
     "height":90 
    }, 
    "medium":{ 
     "url":"https://i.ytimg.com/vi/SSbBvKaM6sk/mqdefault.jpg", 
     "width":320, 
     "height":180 
    }, 
    "high":{ 
     "url":"https://i.ytimg.com/vi/SSbBvKaM6sk/hqdefault.jpg", 
     "width":480, 
     "height":360 
    }, 
    "standard":{ 
     "url":"https://i.ytimg.com/vi/SSbBvKaM6sk/sddefault.jpg", 
     "width":640, 
     "height":480 
    }, 
    "maxres":{ 
     "url":"https://i.ytimg.com/vi/SSbBvKaM6sk/maxresdefault.jpg", 
     "width":1280, 
     "height":720 
    } 
    }, 
    "channelTitle":"emimusic", 
    "tags":[ 
    "Blur", 
    "Song" 
    ], 
    "categoryId":"10", 
    "liveBroadcastContent":"none", 
    "localized":{ 
    "title":"Blur - Song 2", 
    "description":"Blur 21 -- Celebrating 21 years of Blur. To find out more, click here:http://smarturl.it/blur21y\n\n#blur21\n   \nFollow Blur on Twitter:www.twitter.com/blurofficial \nFind Blur on Facebook:www.facebook.com/blur\n\nMusic video by Blur performing Song 2." 
    } 
    }, 
    "contentDetails":{ 
    "duration":"PT2M3S", 
    "dimension":"2d", 
    "definition":"sd", 
    "caption":"false", 
    "licensedContent":true, 
    "regionRestriction":{ 
    "allowed":[ 
     "BY", 
     "US" 
    ] 
    }, 
    "projection":"rectangular" 
    } 
    } 
] 
} 

和这些类。这些课程与Visual Studio 2015选项一样贴得特别。

Public Class Rootobject 
    Public Property kind As String 
    Public Property etag As String 
    Public Property pageInfo As Pageinfo 
    Public Property items() As Item 
End Class 

Public Class Pageinfo 
    Public Property totalResults As Integer 
    Public Property resultsPerPage As Integer 
End Class 

Public Class Item 
    Public Property kind As String 
    Public Property etag As String 
    Public Property id As String 
    Public Property snippet As Snippet 
    Public Property contentDetails As Contentdetails 
End Class 

Public Class Snippet 
    Public Property publishedAt As Date 
    Public Property channelId As String 
    Public Property title As String 
    Public Property description As String 
    Public Property thumbnails As Thumbnails 
    Public Property channelTitle As String 
    Public Property tags() As String 
    Public Property categoryId As String 
    Public Property liveBroadcastContent As String 
    Public Property localized As Localized 
End Class 

Public Class Thumbnails 
    Public Property _default As _Default 
    Public Property medium As Medium 
    Public Property high As High 
    Public Property standard As Standard 
    Public Property maxres As Maxres 
End Class 

Public Class _Default 
    Public Property url As String 
    Public Property width As Integer 
    Public Property height As Integer 
End Class 

Public Class Medium 
    Public Property url As String 
    Public Property width As Integer 
    Public Property height As Integer 
End Class 

Public Class High 
    Public Property url As String 
    Public Property width As Integer 
    Public Property height As Integer 
End Class 

Public Class Standard 
    Public Property url As String 
    Public Property width As Integer 
    Public Property height As Integer 
End Class 

Public Class Maxres 
    Public Property url As String 
    Public Property width As Integer 
    Public Property height As Integer 
End Class 

Public Class Localized 
    Public Property title As String 
    Public Property description As String 
End Class 

Public Class Contentdetails 
    Public Property duration As String 
    Public Property dimension As String 
    Public Property definition As String 
    Public Property caption As String 
    Public Property licensedContent As Boolean 
    Public Property regionRestriction As Regionrestriction 
    Public Property projection As String 
End Class 

Public Class Regionrestriction 
    Public Property allowed() As String 
End Class 

我需要做什么? 在此先感谢。

回答

1

您正在得到该异常,因为您错误地声明了几个自动实现的数组属性。如在Auto-Implemented Properties (Visual Basic)这样的性质示出应被宣布为返回类型的一部分如下,与()阵列指示器:

Public Property items As Item() 

相反,您声明它们如下:

Public Property items() As Item 

这声明一个属性返回一个Item而不是它们的数组。附属于()属性名称是可选的;对于无参数属性而言,它是多余的,但对于采用参数的属性,参数列表就会出现在那里有关详细信息,请参阅文档页面Property Statement。然后,当尝试将JSON数组反序列化为模型中的一个非数组属性时,Json.NET会抛出您看到的异常。

为了解决这个问题,三你的类应作如下修改:

Public Class Rootobject 
    Public Property kind As String 
    Public Property etag As String 
    Public Property pageInfo As Pageinfo 

    Public Property items As Item() ' Fixed 

End Class 

Public Class Snippet 
    Public Property publishedAt As Date 
    Public Property channelId As String 
    Public Property title As String 
    Public Property description As String 
    Public Property thumbnails As Thumbnails 
    Public Property channelTitle As String 

    Public Property tags As String() ' Fixed 

    Public Property categoryId As String 
    Public Property liveBroadcastContent As String 
    Public Property localized As Localized 
End Class 

Public Class Regionrestriction 

    Public Property allowed As String() ' Fixed 

End Class 

然后,由于items是一个数组,访问的标题,并把然后在列表中,你可以使用扩展方法从System.Linq.Enumerable

' Convert the json string to RootObject. 
Dim root = JsonConvert.DeserializeObject(Of Rootobject)(json) 

' Extract the list of titles. 
Dim titles = root.items _ 
    .Select(Function(i) i.snippet.title) _ 
    .ToList() 

' Get the first title in the list. 
Dim firstTitle = titles.FirstOrDefault() 

Console.WriteLine("First title = ""{0}""", firstTitle) 

哪打印出First title = "Blur - Song 2"

样品VB.Net fiddle

+0

谢谢,但是,如何访问“标题”?我没有访问json2(json2.items.snippet.title)中的元素,就像我在我的代码中使用m。在此先感谢 – SomeOneMore

+0

@SomeOneMore - 答案已更新。 – dbc

+0

适合我。谢谢! – SomeOneMore