Deserialize this json in VB.NET - json

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

Related

Extract data from a JSON received from an HTTP response

So I have an HTTP response that gives me this data:
{
"result":"success",
"records":{
"509442013":{
"nif":509442013,
"seo_url":"nexperience-lda",
"title":"Nexperience Lda",
"address":"Rua da Lionesa Nº 446, Edifício G20",
"pc4":"4465",
"pc3":"671",
"city":"Leça do Balio",
"activity":"Desenvolvimento de software. Consultoria em informática. Comércio de equipamentos e sistemas informáticos. Exploração de portais web.",
"status":"active",
"cae":"62010",
"contacts":{
"email":"info#nex.pt",
"phone":"220198228",
"website":"www.nex.pt",
"fax":"224 905 459"
},
"structure":{
"nature":"LDA",
"capital":"5000.00",
"capital_currency":"EUR"
},
"geo":{
"region":"Porto",
"county":"Matosinhos",
"parish":"Leça do Balio"
},
"place":{
"address":"Rua da Lionesa Nº 446, Edifício G20",
"pc4":"4465",
"pc3":"671",
"city":"Leça do Balio"
},
"racius":"http://www.racius.com/nexperience-lda/",
"alias":"Nex - Nexperience, Lda",
"portugalio":"http://www.portugalio.com/nex/"
}
},
"nif_validation":true,
"is_nif":true,
"credits":{
"used":"free",
"left":[
]
}
}
And I need to extract some data from it, like the fields:
NIF
title
and the email and phone that are inside the contacts array.
I need the data to populate some TextBoxes in my Form.
To get the http response I have this button but I don't know how to extract the data I need.
Can someone help me?
My code:
Private Sub Btn_NIFImport_Click(sender As Object, e As EventArgs) Handles Btn_NIFImport.Click
Dim key As String = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
Try
'Create the request
Dim request As HttpWebRequest = HttpWebRequest.Create("http://www.nif.pt/?json=1&q=" & Txt_NIFImport.Text & "&key=" & key)
request.Proxy = Nothing
request.UserAgent = "Test"
'Create the response reader
Dim response As HttpWebResponse = request.GetResponse
Dim responseStream As System.IO.Stream = response.GetResponseStream
'Create a new stream reader
Dim streamReader As New System.IO.StreamReader(responseStream)
Dim data As String = streamReader.ReadToEnd
streamReader.Close()
'Display the data on the screen
Txt_Teste.Text = data
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Thanks in advance.
after you get the response from the http request you need to deserialize the json to an object in vb.net
To Classes to create the object by your json:
Public Class [Structure]
Public Property nature As String
Public Property capital As String
Public Property capital_currency As String
End Class
Public Class Geo
Public Property region As String
Public Property county As String
Public Property parish As String
End Class
Public Class Place
Public Property address As String
Public Property pc4 As String
Public Property pc3 As String
Public Property city As String
End Class
Public Class Record
Public Property nif As Integer
Public Property seo_url As String
Public Property title As String
Public Property address As String
Public Property pc4 As String
Public Property pc3 As String
Public Property city As String
Public Property activity As String
Public Property status As String
Public Property cae As String
Public Property contacts As Contacts
Public Property [structure] As [Structure]
Public Property geo As Geo
Public Property place As Place
Public Property racius As String
Public Property [alias] As String
Public Property portugalio As String
End Class
Public Class Credits
Public Property used As String
Public Property left As List(Of Object)
End Class
Public Class Root
Public Property result As String
Public Property records As Dictionary(Of String, Record)
Public Property nif_validation As Boolean
Public Property is_nif As Boolean
Public Property credits As Credits
End Class
and to deserialize added after you get the response the following line:
Root l = JsonConvert.DeserializeObject<Root>(response);
EDIT:
I made some changes in the code so now you use with Dictionary that has KEY in your json '509442013' and value As Record (Notice that I change the class name as was before '_509442013')
And in the root changes
Public Property records As Dictionary(Of String, Record)
now this will work in each time even if you will get in each http request ID as class that not the same.
notice that the only way you can get the value is by the KEY you already pass in the http request.
Added GIF:
(In my case I change the ID and city, in the result show the city from the json)
Convert Button:
Dim jsonString As String = tbInput.Text
Dim l As Root = JsonConvert.DeserializeObject(Of Root)(jsonString)
tbResult.Text = l.records(tbKEY.Text.ToString).city.ToString()
tbKEY get the value from the screen like in my gif example.

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.

How To Deserialize JSON in vb,net with Multiple objects

Below is a json file that needs to be deserialized and to be stored in different variables.
With this current json, which is returned by an API, I am unable to deserialize because it gives me and error -
Please help me on deserializing this particular json
I have added the code i used, but it returns wrong values and null reference.
{
"result": {
"candidates": [
{
"similarity": 0.1330482513,
"person_id": "75741ea3-4d9b-4e25-8460-16444ee39946",
"descriptor_id": "2f228007-350e-4d58-9897-4b62e9978081",
"user_data": "Без названия (9)",
"external_id": null
}
],
"face": {
"id": "a1b224a3-60c6-4733-9bbc-136d53ea011c",
"score": 0.9320185781
}
},
"timestamp": 1569957900.1488559,
"source": "search",
"event_type": "match",
"authorization": {
"token_id": "71f9b3e0-51b1-480f-93b9-0e76e260bcbc",
"token_data": "first token"
},
"template": {
"descriptor_id": "a1b224a3-60c6-4733-9bbc-136d53ea011c"
},
"candidate": {
"list_id": "6e64e600-cd77-4894-940e-6f7022d8aba8",
"list_data": "FaceStream_search_list(DON'T DELETE)",
"list_type": 1
}
}
I have tried :
Public Class Rootobject
Public Property result As Result
Public Property timestamp As Single
Public Property source As String
Public Property event_type As String
Public Property authorization As Authorization
Public Property template As Template
Public Property candidate As Candidate1
End Class
Public Class Result
Public Property candidates() As Candidate
Public Property face As Face
End Class
Public Class Face
Public Property id As String
Public Property score As Single
End Class
Public Class Candidate
Public Property similarity As Double
Public Property person_id As String
Public Property descriptor_id As String
Public Property user_data As String
Public Property external_id As Object
End Class
Public Class Authorization
Public Property token_id As String
Public Property token_data As String
End Class
Public Class template
Public Property descriptor_id As String
End Class
Public Class Candidate1
Public Property list_id As String
Public Property list_data As String
Public Property list_type As Integer
End Class
''And used
Dim Candidatess As New Candidate
Candidatess = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Candidate)((JObject.Parse(e.Message)).ToString)
msg(Candidatess.similarity.ToString)
msg(Candidatess.descriptor_id.ToString)
msg(Candidatess.person_id.ToString)
''REturns me Null
Public Class Candidate
Public Property similarity As Double
Public Property person_id As String
Public Property descriptor_id As String
Public Property user_data As String
Public Property external_id As Object
End Class
Public Class Face
Public Property id As String
Public Property score As Double
End Class
Public Class Result
Public Property candidates As Candidate()
Public Property face As Face
End Class
Public Class Authorization
Public Property token_id As String
Public Property token_data As String
End Class
Public Class Template
Public Property descriptor_id As String
End Class
Public Class Candidate
Public Property list_id As String
Public Property list_data As String
Public Property list_type As Integer
End Class
Public Class Candidates
Public Property result As Result
Public Property timestamp As Double
Public Property source As String
Public Property event_type As String
Public Property authorization As Authorization
Public Property template As Template
Public Property candidate As Candidate
End Class
Public Function GetData(ApiEndpoint As Uri, ApiToken As String)
Dim origResponse As HttpWebResponse = Nothing
Dim objResponse As HttpWebResponse = Nothing
Dim origReader As StreamReader = Nothing
Dim objReader As StreamReader = Nothing
Dim origRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(ApiEndpoint), HttpWebRequest)
origRequest.Headers.Add("Authorization", "Basic " & ApiToken)
origRequest.AllowAutoRedirect = False
origRequest.Method = "GET"
'Call the API and get the JSON Response data
origResponse = DirectCast(origRequest.GetResponse(), HttpWebResponse)
Dim Stream As Stream = origResponse.GetResponseStream()
Dim sr As New StreamReader(Stream, Encoding.GetEncoding("utf-8"))
Dim myJsonResponse As String = sr.ReadToEnd()
'Deserialize the Json
Dim objFormResults As Result = JsonConvert.DeserializeObject(Of Result)(myJsonResponse)
'loop through the results
End Function

Generating Class VB.NET

I was using JSON Utils to generate a Class and I came to a problem. The JSON string I'm using:
{
"type": "champion",
"version": "7.16.1",
"data": {
"1": {
"title": "the Dark Child",
"id": 1,
"key": "Annie",
"name": "Annie"
}
}
}
The Class it generates:
Public Class 1
Public Property title As String
Public Property id As Integer
Public Property key As String
Public Property name As String
End Class
Public Class Data
Public Property 1 As 1
End Class
Public Class Example
Public Property type As String
Public Property version As String
Public Property data As Data
End Class
The thing is that I cannot name a Class 1, I am still trying to find a solution but with no luck yet. Is there a work around this?
I think it should be change to Dictionary
Public Class Example
Public Property type As String
Public Property version As String
Public Property data As Dictionary(Of String, NumberType)
End Class
Public Class NumberType
Public Property title As String
Public Property id As Integer
Public Property key As String
Public Property name As String
End Class

Cannot Json deserialize Youtube data with VB.net

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.