Iterating through Twitch Json - json

I'm not very good at this yet, not sure if my subject even accurately describes what I need. I know it's probably been answered, I'm having a hard time understanding the answers since they don't directly apply to my data. I am trying to figure out how I can iterate through the data in this URL.
http://tmi.twitch.tv/group/user/twitch/chatters
This is what I've been using.
Dim url = "http://tmi.twitch.tv/group/user/" & ConnectionInformation.Channel.TrimStart("#") & "/chatters"
Dim json As String = Nothing
Dim wc As New WebClient()
json = wc.DownloadString(url)
Dim root As JToken = JToken.Parse(json)
For Each item As JToken In root("chatters")
'I've tried several things here and I can't find a good way to iterate through the viewers found here.
Next
I guess I'm having trouble getting the viewers in a collection so that I can iterate through them, can someone point me in the right direction here?
Thank you.

"Chatters" is actually a Type in the root object. If you were to create classes, they would look like this:
Public Class RootChatter
Public Property _links As _Links
Public Property chatter_count As Integer
Public Property chatters As Chatters
End Class
Public Class _Links
End Class
Public Class Chatters
Public Property moderators As String()
Public Property staff As String()
Public Property admins As String()
Public Property global_mods As String()
Public Property viewers As String()
End Class
Viewers is an array in the Chatters Property (Root.Chatters.Viewers). Without the class:
Dim root As JToken = JToken.Parse(jstr)
Dim chatters = root("chatters")("viewers")
For n As Integer = 0 To chatters.Count - 1
Console.WriteLine(chatters(n))
Next
Output:
04paynem
0morningstar0
0rchlann
0riginus
10108abc
If you were to deserialize to the classes:
Dim jc = JsonConvert.DeserializeObject(Of RootChatter)(jstr)
Dim viewers = jc.chatters.viewers

Related

Deserialize JSON string (without root)to object VB.NET

I,
i need to deserialize a sting that contain JSON formatted data but without root element
the data(simplified!) are this
[{"ID":"974",
"DataIns":"2022-08-12 14:13:26",
"NumeroFattura":"CTD18473",
"DataFattura":"2022-08-08",
"RagSocMit":"Example1"},
{"ID":"973",
"DataIns":"2022-08-12 13:31:00",
"NumeroFattura":"CTCC10189",
"DataFattura":"2022-08-08",
"RagSocMit":"Example2"},
{"ID":"971",
"DataIns":"2022-08-09 15:30:29",
"NumeroFattura":"C18474",
"DataFattura":"2022-08-08",
"RagSocMit":"Example2"}]
and the class for deserializing are this
Public Class TestClass
Public Property ID As String
Public Property DataIns As String
Public Property NumeroFattura As String
Public Property DataFattura As String
Public Property RagSocMit As String
End Class
If i try to deserialize with this code
Dim result As String = ""
Dim ListaFatture As FattureAC2
...
result = reader.ReadToEnd'contain the sting json from a WS
ListaFatture = JsonConvert.DeserializeObject(Of IEnumerable(Of FattureAC2))(result)
ListaFatture are equal to NOTHING
Someone can help me?
Tnx
Salvo
you can not deserialize as IEnumerable, you have to select what do you need an array or list for example
Dim ListaFatture As List (Of TestClass)
ListaFatture = JsonConvert.DeserializeObject(Of List(Of TestClass))(result)

JSON.Net Deserialization error, My Class definition is wrong?

In VB.Net program I'm getting the "Cannot deserialize the current JSON array" when using JsonConvert.DeserializeObject(Of MCMusicElements)(sRB).
The JSON array I'm working with is:
[{"Key":465419,"MIK_Energy":3,"MIK_Camelot":"9B","MIK_BPM":118}]
If I delete the "[ ]" in the JSON string my program works. So, I'm assuming my Class definition for MCMusicElements is wrong in some way. I'd like to understand how to make it work without deleting the brackets from the JSON string.
Public Class MCMusicElements
Public Property Key As Integer
Public Property MIK_Energy As Integer
Public Property MIK_Camelot As String
Public Property MIK_BPM As Integer
End Class
Dim oResult = JsonConvert.DeserializeObject(Of MCMusicElements)(sRB)
dtDataTable.Rows(index).Item(1) = oResult.MIK_Energy
dtDataTable.Rows(index).Item(2) = oResult.MIK_Camelot
dtDataTable.Rows(index).Item(3) = oResult.MIK_BPM
you have to use list of objects, not an object
Dim oResult = JsonConvert.DeserializeObject(Of List(Of MCMusicElements))(sRB)
dtDataTable.Rows(index).Item(1) = oResult(0).MIK_Energy

json key with array of values - how to parse

This is the json data I am trying to parse. (I did trim the imagedata down for example purposes)
{"imageData":["SUkqAORlAACGniG0JCHeSTV9icwWxF+N9AwzcsTDlLu+PeYCgeZXAP//","sfsdfsdyfhh2h43h8ysdfsdnvjknjfdsfdsf"]}
Any idea on how to parse it into a strongly typed class in .NET?
I am using the newtonsoft.json
I tried the following
Public Class DAFGImages
Public imageData As List(Of String)
End Class
Dim DAFGImages As List(Of DAFGImages) = Newtonsoft.Json.JsonConvert.DeserializeObject(json, GetType(List(Of DAFGImages)))
Your class already contains a List of string, so you dont need a list of DAFGImages too. I would change the class member to a property:
Public Class DAFGImages
Public Property imageData As List(Of String)
End Class
Then:
Dim jstr = ... from wherever
Dim myImgs = Newtonsoft.Json.JsonConvert.DeserializeObject(Of DAFGImages)(jstr)
myImgs.ImageData will contain the 2 elements.

VB.Net Need some tips for using JavaScriptSerializer to Deserialize JSON string

First question asked here, it is nice to be part of a coding community!
Currently I am retrieving a JSON string from an API and the response is as follows:
[{"playerId":37067559,"championId":78,"championLevel":5,"championPoints":93023,"lastPlayTime":1454133232000,"championPointsSinceLastLevel":71423,"championPointsUntilNextLevel":0},{"playerId":37067559,"championId":105,"championLevel":5,"championPoints":39025,"lastPlayTime":1454130615000,"championPointsSinceLastLevel":17425,"championPointsUntilNextLevel":0},{"playerId":37067559,"championId":81,"championLevel":5,"championPoints":37068,"lastPlayTime":1454273384000,"championPointsSinceLastLevel":15468,"championPointsUntilNextLevel":0}]
I set the string to be "response2"
Dim response2 As String = serviceRequest.DownloadString(New Uri(Mastery))
However in using the JavaScriptSerializer the code exits my console
Dim jss As New JavaScriptSerializer()
Dim model As MyModel = jss.Deserialize(Of MyModel)(response2)
Console.Write(model.championId(0))
Console.ReadLine()
I set the model for the Deserializer aswell
Public Class MyModel
Public Property playerId() As String
Public Property championId() As String
Public Property championPoints() As String
Public Property lastPlayTime() As String
Public Property championPointsSinceLastLevel() As String
Public Property championPointsUntilNextLevel() As String
End Class
However on startup the console exits itself. I'm not sure if I'm supposed to use model.championId(0) aswell, I assume to becuase there is more than one set of championId.
You need array of MyModel to hold the serialized objects
Try this (comment in code):
Dim jss As New JavaScriptSerializer()
' use model() to hold aray of MyModel
Dim model() As MyModel = jss.Deserialize(Of MyModel())(response2)
'As model is now array of MyModel you should use model(0), model(1) etc...
Console.Write(model(0).championId())

ServiceStack.Text reading json results not working

I am just trying to figure out the best way to deserialize a json string returned from a 3rd party api call. I read ServiceStack is fast so want to try it out. No experience and here is what I have done:
Opened Visual Studio 2013
Created new project Windows Forms Application
Installed ServiceStack.Text (based on https://servicestack.net/download)
Added a button (btnView) and textbox (txtOutput)
Add code to btnView_Click event
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
Me.Cursor = Cursors.WaitCursor
Dim wp As New WebPost 'this allows to pass url and return results
wp.URL = "xxxx"
Dim sJSONRetVal As String = wp.Request(String.Empty, True)
'sJSONRetVal return values looks like the following:
'{"complaints":[{"feedback_type":"abuse","subject":"Sales Agent Position"},{"feedback_type":"abuse","subject":"Sales Agent Position"}],"message":"OK","code":0}
'ServiceStack.Text example
Dim t As SMTP_Complaints = ServiceStack.Text.JsonSerializer.DeserializeFromString(Of SMTP_Complaints)(sJSONRetVal)
'For Each xi As SMTP_Complaints In t
' txtOutput.Text &= xi.mail_from & vbCrLf
'Next
wp = Nothing
txtOutput.Text = t.ToString
Me.Cursor = Cursors.Default
End Sub
Public Class SMTP_Complaints
Dim _feedback_type As String = ""
Dim _subject As String = ""
Public Property feedback_type As String
Get
Return _feedback_type
End Get
Set(value As String)
_feedback_type = value
End Set
End Property
Public Property subject As String
Get
Return _subject
End Get
Set(value As String)
_subject = value
End Set
End Property
End Class
The above doesn't seem to get any data. how would I loop through the data returned and return the data from both instances? Just not sure how I need to set this up to read the json data and then be able to output.
Based on the returned JSON of:
{"complaints":[{"feedback_type":"abuse","subject":"Sales Agent Position"},{"feedback_type":"abuse","subject":"Sales Agent Position"}],"message":"OK","code":0}
You will need two DTOs to deserialise this result.
I have used auto implemented properties here to simplify the complexity of the code. If you use an older version of VB, you'll need to expand these out to include a backing field with get and set method.
Public Class SMTP_Complaint
Public Property feedback_type As String
Public Property subject As String
End Class
Public Class SMTP_ComplaintsResponse
Public Property complaints As SMTP_Complaint()
Public Property message As String
Public Property code As Integer
End Class
You need the SMTP_ComplaintsResponse class because your complaints are wrapped in your JSON response.
Then to deserialise the response:
Dim response = JsonSerializer.DeserializeFromString(Of SMTP_ComplaintsResponse)(sJSONRetVal)
And your complaints are then accessible:
For Each complaint As var In response.complaints
Console.WriteLine("Type: {0}, Subject {1}", complaint.feedback_type, complaint.subject)
Next