I am trying to use the JavaScriptSerializer
to pull some information from a website API. However, I am new to this in vb.net and can't seem to get the textbox to load. I think it has to do with the JSON text I am receiving from the website where it has a status at the top of it, but I don't know how to get around this.
Here is the JSON text:
{
"status": "ok",
"meta": {
"count": 1
},
"data": {
"1000003149": {
"members": [
{
"account_name": "CHOP210"
},
{
"account_name": "Guns_n_Roses"
},
{
"account_name": "vonhames"
},
{
"account_name": "wiggum99"
},
{
"account_name": "VonChalon"
},
{
"account_name": "FokkerDR1"
},
{
"account_name": "Force12"
},
{
"account_name": "m4irish"
},
{
"account_name": "panzer1_14"
},
{
"account_name": "alchemicalgunner007"
},
{
"account_name": "Morrros"
},
{
"account_name": "jonah128"
},
{
"account_name": "Zy4"
},
{
"account_name": "Terminator6644"
},
{
"account_name": "Dionisiovega"
}
]
}
}
}
Here is my code:
Imports System.Net
Imports System.IO
Imports System.Web.Script.Serialization
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uriString As String = "https://api.worldoftanks.com/wot/clans/info/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&clan_id=1000003149&fields=members.account_name"
Dim uri As New Uri(uriString)
Dim Request As HttpWebRequest = HttpWebRequest.Create(uri)
Request.Method = "GET"
Dim Response As HttpWebResponse = Request.GetResponse()
Dim Read = New StreamReader(Response.GetResponseStream())
Dim Raw As String = Read.ReadToEnd()
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of List(Of Object))(Raw)
For Each item As Object In dict
TextBox1.Text += item("account_name").ToString + vbNewLine
Next
End Sub
End Class
I think the reason the textbox will not load is because of the status and data portion of the JSON text. Right now I am just filling a multi-line textbox but I will need to bind these to a combobox later with the account ID.
The problem is you are trying to deserialize the JSON as a List(Of Object)
but the JSON is not actually a list; instead, it is a series of nested objects, with the innermost object containing the list of members. (I've reformatted the JSON in your question to make it more obvious how things are structured.) You need to create classes to model the JSON and then deserialize into that model.
One oddity in your JSON is the 1000003149
key, which I assume is dynamic (looks like this corresponds to the clan ID in the URL). So you will need a dictionary to handle that part.
Here are the classes that correspond to the JSON:
Public Class RootObject
Public Property status As String
Public Property meta As Meta
Public Property data As Dictionary(Of String, Clan)
End Class
Public Class Meta
Public Property count As Integer
End Class
Public Class Clan
Public Property members As List(Of Member)
End Class
Public Class Member
Public Property account_name As String
End Class
Then, you can deserialize and fill your TextBox like this:
Dim jss As New JavaScriptSerializer
Dim root As RootObject = jss.Deserialize(Of RootObject)(Raw)
For Each member As Member In root.data.First().Value.members
TextBox1.Text += member.account_name + vbNewLine
Next