VB.NET how to get session id out of JSON string

51 views Asked by At

i'm trying to get the session id out of a string looking like this:

{"url":"","port":1337,"status":"OK kPzKTe0SNYGRWsPxoiK8jg5qllvPLn7hG4MRFSjqU60w4525175","session":"kPzKTe0SNYGRWsPxoiK8jg5qllvPLn7hG4MRFSjqU60w4525175","username":"foobar","secret":"1c25687b71f183e53009dd0156becfe5","user_id":1337,"vip":0}

it's the id after "session": kPzKTe0SNYGRWsPxoiK8jg5qllvPLn7hG4MRFSjqU60w4525175

i tried alot with split and stuff but couldnt get it done so far. any ideas? btw: the output isnt always same order. url status session change positions each time i request it.

Function MySplitFunction()
    FrmConsole.Console.Text = FrmConsole.Console.Text & "[API] getting sid" & vbCrLf


    Dim client As New WebClient
    Dim sessionid As String = client.DownloadString("https://www.spin.de/api/login?login=usr;pw=pass;server=1337")
    Dim Splitted() As String = sessionid.Split(New String() {"""session""", ","}, StringSplitOptions.None)


    FrmConsole.Console.Text = FrmConsole.Console.Text & "SID: " & Splitted(1) & vbCrLf

End Function
1

There are 1 answers

3
SSS On BEST ANSWER

Copy your JSON text to the clipboard then use Edit>Paste Special>Paste JSON as Classes to create your class. Then use System.Text.Json to deserialize:

Imports System.Text.Json

Module Program
  Sub Main(args As String())
    Dim myJsonString = "{""url"":"""",""port"":1337,""status"":""OK kPzKTe0SNYGRWsPxoiK8jg5qllvPLn7hG4MRFSjqU60w4525175"",""session"":""kPzKTe0SNYGRWsPxoiK8jg5qllvPLn7hG4MRFSjqU60w4525175"",""username"":""foobar"",""secret"":""1c25687b71f183e53009dd0156becfe5"",""user_id"":1337,""vip"":0}"
    Dim myJsonObject = JsonSerializer.Deserialize(Of MyJsonClass)(myJsonString)
    Console.WriteLine(myJsonObject.session)
    Console.WriteLine("Finished.")
    Console.ReadLine()
  End Sub
End Module

Public Class MyJsonClass
  Public Property url As String
  Public Property port As Integer
  Public Property status As String
  Public Property session As String
  Public Property username As String
  Public Property secret As String
  Public Property user_id As Integer
  Public Property vip As Integer
End Class