You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET如何从JSON字符串中提取Session ID

解决VB.NET中从JSON提取Session ID的问题

字符串分割的方式完全依赖JSON字段的固定顺序,而JSON本身不保证字段顺序不变,这是你当前方案失效的核心原因。正确的做法是使用JSON解析库来结构化处理数据,以下是两种可靠的解决方案:


方案1:使用.NET内置的System.Text.Json(适用于.NET Core 3.0+/NET 5+)

无需额外安装库,直接利用框架自带的JSON处理能力:

方式A:定义模型类(类型安全)

先创建一个与JSON响应结构匹配的类(只需保留你需要的字段即可):

Public Class LoginApiResponse
    Public Property session As String
    ' 若需要其他字段可在此添加,比如username、status等
End Class

然后修改你的函数:

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

    Dim client As New WebClient
    Dim jsonResponse As String = client.DownloadString("https://www.xxx.de/api/login?login=usr;pw=pass;server=1337")

    ' 反序列化JSON到模型类
    Dim apiResult As LoginApiResponse = System.Text.Json.JsonSerializer.Deserialize(Of LoginApiResponse)(jsonResponse)
    Dim sessionId As String = apiResult.session

    FrmConsole.Console.Text = FrmConsole.Console.Text & "SID: " & sessionId & vbCrLf
End Function

方式B:直接解析(无需模型类)

如果不想定义类,也可以直接遍历JSON结构:

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

    Dim client As New WebClient
    Dim jsonResponse As String = client.DownloadString("https://www.xxx.de/api/login?login=usr;pw=pass;server=1337")

    Using doc As System.Text.Json.JsonDocument = System.Text.Json.JsonDocument.Parse(jsonResponse)
        Dim sessionId As String = doc.RootElement.GetProperty("session").GetString()
        FrmConsole.Console.Text = FrmConsole.Console.Text & "SID: " & sessionId & vbCrLf
    End Using
End Function

方案2:使用Newtonsoft.Json(Json.NET,适用于所有.NET版本)

这是.NET生态中广泛使用的JSON库,兼容性更强,适合.NET Framework等旧版本:

  1. 首先通过NuGet包管理器安装Newtonsoft.Json
  2. 使用以下代码提取Session ID:

方式A:动态解析

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

    Dim client As New WebClient
    Dim jsonResponse As String = client.DownloadString("https://www.xxx.de/api/login?login=usr;pw=pass;server=1337")

    Dim apiResult As Object = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonResponse)
    Dim sessionId As String = apiResult.session

    FrmConsole.Console.Text = FrmConsole.Console.Text & "SID: " & sessionId & vbCrLf
End Function

方式B:JObject解析

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

    Dim client As New WebClient
    Dim jsonResponse As String = client.DownloadString("https://www.xxx.de/api/login?login=usr;pw=pass;server=1337")

    Dim jObj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonResponse)
    Dim sessionId As String = jObj("session").ToString()

    FrmConsole.Console.Text = FrmConsole.Console.Text & "SID: " & sessionId & vbCrLf
End Function

核心优势

  • 完全不受JSON字段顺序变化影响,始终能精准定位session字段
  • 自动处理JSON格式的合法变化(如空格、换行),稳定性远高于字符串分割
  • 类型安全的模型类方式能避免字符串处理中的潜在错误

内容的提问来源于stack exchange,提问作者xeno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 17:17:32