0

i need some help. I wanna make a program uses the website to search for data,those text come in JSON format,how do i load those files and write them on labels? like this > {"mature":null,"status":"Chill","broadcaster_language":"en","display_name":"MexxHD","game":"Counter-Strike: Global Offensive"}}

Here is the link i use for read this > https://api.twitch.tv/kraken/channels/MexxHD

I was able to get an "answer" from the website,but i dont know how to print them in labels(here is code what i use)

Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://api.twitch.tv/kraken/channels/" & TextBox1.Text), HttpWebRequest)
    Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    Dim reader As New StreamReader(response.GetResponseStream())

How do i from this > {"mature":null,"status":"Chill","broadcaster_language":"en".. Move information in labels like Labe1.text to be Chill and label2.text to be en...

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Mexx
  • 1
  • 1

2 Answers2

1

A JSON string is not just a complex string - it is serialized data (a class, dictionary, array etc). Nor can you ignore the parts you don't care about: your post leaves off a big chunk at the end.

In this case, it looks like it can be deserialized to a Dictionary, with the last element ("_links") itself being a dictionary. Looking at the text, the first element ("mature") is null, so I use Dictionary(Of String, Object):

Imports Newtonsoft.Json

' I downloaded it for ease, you could use the response
Dim json = File.ReadAllText("C:\Temp\MexxHD.json")

Dim col As Dictionary(Of String, Object) 
col = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(json)

That's it really. Display the contents (remember at least one value is Nothing):

For Each kvp As KeyValuePair(Of String, Object) In col
    Console.WriteLine("Key: {0}, value: {1}", kvp.Key,
                      If(kvp.Value Is Nothing, "Null", kvp.Value.ToString))
Next

Output:

Key: mature, value: Null
Key: status, value: Chill
Key: broadcaster_language, value: en
Key: display_name, value: MexxHD
(etc)

The links part can be deserialized to its own dictionary:

Dim JLinks = col("_links").ToString
Dim linkCol As Dictionary(Of String, String) 
linkCol = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(JLinks)

Console.WriteLine("********* LINKS ********* ")
For Each kvp As KeyValuePair(Of String, String) In linkCol
    Console.WriteLine("K: {0} V: {1}", kvp.Key, kvp.Value)
Next

Output:

********* LINKS *********
K: self V: https://api.twitch.tv/kraken/channels/mexxhd
K: follows V: https://api.twitch.tv/kraken/channels/mexxhd/follows
K: commercial V: https://api.twitch.tv/kraken/channels/mexxhd/commercial
(etc)

When the JSON appears to be an array of class objects, there are online tools which will create the class structure from the JSON string such as this one which will do VB classes

Just fetch the items you want from the Dictionary. Do keep in mind that some values can be Nothing as is the case for "mature" which can lead to a NullReference Exception:

Label1.Text = col("status").ToString()
Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
-1

First do this

Imports Newtonsoft.Json.Linq

Second Add Reference > Newtonsoft.Json.dll

And Third use this code

Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader    

    request = DirectCast(WebRequest.Create("replace with your WEB LINK, HttpWebRequest)
                response = DirectCast(request.GetResponse(), HttpWebResponse)
                reader = New StreamReader(response.GetResponseStream())

                Dim rawresp As String
                rawresp = reader.ReadToEnd()
                Dim jResults As JObject = JObject.Parse(rawresp)
                '===============LABELS TEXT===========================
                Label1.Text = jResults(" replace with your token ").ToString()

And Last One,Replace with your informations

Mexx
  • 1
  • 1