2017-07-29 28 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