Cannot Json deserialize Youtube data with VB.net - json

I need to obtain the "title" from the response i get from Youtube.
I get the following error at line 8, position 12, that is, at line:
"items": [
just after the "["
The error i get is:
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>
I have this code:
Dim m As IEnumerable(Of Rootobject) = JsonConvert.DeserializeObject(Of IEnumerable(Of Rootobject))(res)
with this JSON data:
{
"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"
}
}
]
}
and these classes. the classes are pasted as special with the visual studio 2015 option.
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
What i need to do?
Thanks in advance.

You are getting that exception because you have declared several auto-implemented array properties incorrectly. As shown in Auto-Implemented Properties (Visual Basic) such properties should be declared as follows, with the () array indicator as part of the return type:
Public Property items As Item()
Instead you declare them as follows:
Public Property items() As Item
This declares a property returning a single Item rather than an array of them. The () attached to the property name is optional; for parameterless properties it is redundant but for properties taking parameters the argument list appears there. See the documentation page Property Statement for details. Then Json.NET throws the exception you see when attempting to deserialize a JSON array into one of the non-array properties in your model.
To fix this, three of your classes should be modified as follows:
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
Then, since the items are an array, to access the titles and put then in a list, you can use extension methods from 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)
Which prints out First title = "Blur - Song 2".
Sample VB.Net fiddle.

Related

Visual Basic: Deserializing a Json containing an array fails

I tried to deserialize the following Json:
{
"Compensations":
[
{"Name": "Compensation_01"},
{"Name": "Compensation_02"}
]
}
paste special => paste json as classes results in
Public Class Rootobject
Public Property Compensations() As Compensation
End Class
Public Class Compensation
Public Property Name As String
End Class
both NewtonSoft:
Dim cmpnstn = JsonConvert.DeserializeObject(Of Rootobject)(json)
and Microsoft:
Dim cmpnstn = JsonSerializer.Deserialize(Of Rootobject)(json)
throw exceptions:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ...
System.Text.Json.JsonException: 'The JSON value could not be converted to Path: ... LineNumber: ... BytePositionInLine:
Public Property Compensations() As Compensation result in the IL Code (no array)
public Compensation Compensations
{
get;
set;
}
When I change the property to
Public Property Compensations As Compensation()
I get the IL code for an array
public Compensation[] Compensations
{
get;
set;
}
My question is: Why does that paste special do that form of property declaration
and what is the meaning of that Compensations() As Compensation.
change
Public Property Compensations() As Compensation
to
Public Property Compensations As Compensation()
The latter solves the problem.

Deserializer is not parsing the JSON string

My class is not being populated with the JSON string
Using the VS 2012 Edit -> Paste Special -> JSON as a CLASS
The Class Created:
Public Class clsMCParcelData
Public Class Rootobject
Public Property ParcelFound As Boolean
Public Property ParcelInfo As New APN
End Class
Public Class APN
Public Property Book As String
Public Property Map As String
Public Property Item As String
Public Property clean As String
Public Property formatted As String
Public Property link As String
End Class
END CLASS
JSON String:
{
"ParcelFound":true,
"APN":{
"Book":"510",
"Map":"11",
"Item":"668",
"clean":"51011668",
"formatted":"510-11-668",
"link":"\/api\/parcel\/51011668"
}
}
Code on the asp.vb page:
Dim jss As New JavaScriptSerializer, MCD As New clsMCParcelData.Rootobject
MCD = jss.Deserialize(Of clsMCParcelData.Rootobject)(raw)
I would expect that once deserialized, I would be able to access the APN information like so:
tbParcelNumber.text = MCD.ParcelInfo.formatted (returning 510-11-668)
What I get is Nothing as the value of MCD.ParcelInfo.formatted
The JavaScriptSerializer requires an exact match in the class to deserialize.

Deserialize this json in VB.NET

I have this json response:
{
"tracked_until": "1483704963",
"solo_competitive_rank": "4066",
"competitive_rank": "3821",
"mmr_estimate": {
"estimate": 3971,
"stdDev": 215.26495302301302,
"n": 20
},
"profile": {
"account_id": 131505839,
"personaname": "LeG",
"name": null,
"cheese": 1,
"steamid": "76561198091771567",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c.jpg",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c_medium.jpg",
"avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c_full.jpg",
"profileurl": "http://steamcommunity.com/id/LegLabs/",
"last_login": "2016-11-11T13:13:18.651Z",
"loccountrycode": "AL"
}
}
Using an Online Tool, I created these classes:
<Serializable>
Public Class mmr_estimate
Public Property estimate As String
Public Property stdDev As String
Public Property n As String
End Class
<Serializable>
Public Class profile
Public Property account_id As String
Public Property personaname As String
Public Property name As String
Public Property cheese As String
Public Property steamid As String
Public Property avatar As String
Public Property avatarmedium As String
Public Property avatarfull As String
Public Property profileurl As String
Public Property last_login As String
Public Property loccountrycode As String
End Class
<Serializable>
Public Class RootObject
Public Property tracked_until As String
Public Property solo_competitive_rank As String
Public Property competitive_rank As String
Public Property mmr_estimate As mmr_estimate
Public Property profile As profile
End Class
Then I use this code to deserialize it:
Dim steamData As String = ' the json contents above
Dim myjss As New JavaScriptSerializer()
Dim playerDictionary = myjss.Deserialize(Of List(Of RootObject))(steamData)
But the result I get is nothing, playerDictionary has 0 items, when it should have 1 item with the contents of the json parsed into KeyValuePairs.
If I use this piece of code
Dim data = myjss.DeserializeObject(steamData)
and then run a for each loop on the data elements, I can see the contents of data when debugging, but I don't know how to work with them like that, since they are just objects which I'm having trouble converting into KeyValuePairs, who in themselves may contain arrays of KeyValuePairs.
What I'm trying to get is the values of solo_competitive_rank, competitive_rank and steamid, but if I can't get the whole contents deserialized, I can't do that.
Are the declared classes wrong?
but with this solution you use not the class RootObject.
With first method, your JSON want in the list with key and value.
This is only for JSON Array. :(
rg

How do I parse JSON children using JSON.NET?

I’m trying to get the results in textures: […] in JSON obtained from
http://t0.rbxcdn.com/ca063a6bab4e145de481384a8a62d64f and display it in a listbox.
I have this code:
Public Class ReturnObject
Public Property Url As String
Public Property textures As String
Public Property obj As String
Public Property mtl As String
Public Property Final As Boolean
End Class
Dim input2 As String = obj.Text
Dim json2 As ReturnObject = JsonConvert.DeserializeObject(Of ReturnObject)(input2)
obj.Text = HttpGet("http://www.roblox.com/thumbnail/resolve-hash/" + json2.obj)
mtl.Text = HttpGet("http://www.roblox.com/thumbnail/resolve-hash/" + json2.mtl)
But I get the error
Error reading string. Unexpected token: StartArray. Path 'textures',
line 1, position 313.
textures is an array in the JSON, containing one string (mind the brackets*):
"textures":["bff5714f0b959385663b1fe030b87064"]
You can reflect that in your class structure (mind the brackets**):
Public Class ReturnObject
Public Property Url As String
Public Property textures As String()
Public Property obj As String
Public Property mtl As String
Public Property Final As Boolean
End Class
* they’re square brackets
** they’re parentheses

Deserialized JSON data returning properties of "Nothing" in VB.NET

This has been driving me mad for the past few days. I am connecting to the Shopify API and downloading JSON data. I have been able to successfully deserialize the data to an object using the class and code below. However, the object "customer" comes back with properties (as per the class) equal to "Nothing". What is going on? Any help would be greatly appreciated!! P.S. I should mention I am using Newtonsoft JSON.NET.
Partial Public Class ParsedJSON
Public Class Customer2
Public Property accepts_marketing As Boolean
Public Property created_at As DateTime
Public Property email As String
Public Property first_name As String
Public Property id As Integer
Public Property last_name As String
Public Property last_order_id As Integer
Public Property multipass_identifier As Object
Public Property note As Object
Public Property orders_count As Integer
Public Property state As String
Public Property total_spent As String
Public Property updated_at As DateTime
Public Property verified_email As Boolean
Public Property tags As String
Public Property last_order_name As String
Public Property default_address As DefaultAddress
End Class
End Class
Partial Public Class ParsedJSON
Public Class Order
Public Property buyer_accepts_marketing As Boolean
Public Property cancel_reason As Object
Public Property cancelled_at As Object
Public Property cart_token As String
Public Property checkout_token As String
Public Property closed_at As Object
Public Property confirmed As Boolean
Public Property created_at As DateTime
Public Property currency As String
Public Property email As String
Public Property financial_status As String
Public Property fulfillment_status As Object
Public Property gateway As String
Public Property id As Integer
Public Property landing_site As String
Public Property location_id As Object
Public Property name As String
Public Property note As String
Public Property number As Integer
Public Property reference As Object
Public Property referring_site As String
Public Property source As String
Public Property source_identifier As Object
Public Property source_name As String
Public Property source_url As Object
Public Property subtotal_price As String
Public Property taxes_included As Boolean
Public Property test As Boolean
Public Property token As String
Public Property total_discounts As String
Public Property total_line_items_price As String
Public Property total_price As String
Public Property total_price_usd As String
Public Property total_tax As String
Public Property total_weight As Integer
Public Property updated_at As DateTime
Public Property user_id As Object
Public Property browser_ip As Object
Public Property landing_site_ref As Object
Public Property order_number As Integer
Public Property discount_codes As Object()
Public Property note_attributes As Object()
Public Property processing_method As String
Public Property checkout_id As Integer
Public Property tax_lines As TaxLine()
Public Property tags As String
Public Property line_items As LineItem()
Public Property shipping_lines As ShippingLine()
Public Property billing_address As BillingAddress
Public Property shipping_address As ShippingAddress
Public Property fulfillments As Object()
Public Property client_details As ClientDetails
Public Property customer As Customer2
End Class
End Class
Partial Public Class ParsedJSON
Public Property orders As Order()
End Class
... You get the idea
Now, here is my implementation code:
' Calls the method GetResponseStream to return the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
' Pipes the response stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, encode)
Dim json_data As String = readStream.ReadToEnd()
Dim serializer As New JavaScriptSerializer()
Dim customer As ParsedJSON.Customer2 = JsonConvert.DeserializeObject(Of ParsedJSON.Customer2)(json_data)
An example of the JSON data can be seen at http://docs.shopify.com/api/order.
Code defensively. It is a well known issue that Shopify can provide Orders via the API that have null customer records. If you run into one of those, provide some safe defaults. Webhooks can receive orders with null customers too.
And when you fix your problem that bums out with valid customers, all will be well for you.