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

如何在VB.NET中从POST API响应消息提取sadadNumber数据

提取JSON响应中的sadadNumber字段值

在VB.NET中,你可以通过两种常用方式解析JSON响应并提取目标字段:


方法一:使用System.Text.Json(官方内置库)

1. 定义JSON对应的实体类

先创建与响应JSON结构匹配的类,方便规范地反序列化:

Public Class ApiResponse
    Public Property status As Integer
    Public Property message As String
    Public Property data As ResponseData
End Class

Public Class ResponseData
    Public Property billNumber As String
    Public Property sadadNumber As String
End Class

2. 修改原代码解析响应

在你获取returnValue的位置,添加反序列化逻辑:

Dim returnValue As String = result.Content.ReadAsStringAsync().Result
If result.StatusCode <> 200 Then
    Throw New Exception($"Failed to POST data: ({result.StatusCode}): {returnValue}")
Else
    Debug.Print(returnValue)
    ' 解析JSON并提取sadadNumber
    Dim response = System.Text.Json.JsonSerializer.Deserialize(Of ApiResponse)(returnValue)
    Dim sadadNumber = response.data.sadadNumber
    Debug.Print($"提取到的sadadNumber: {sadadNumber}") ' 此处可将变量用于后续业务处理
End If

方法二:使用Newtonsoft.Json(Json.NET,更灵活)

如果不想定义实体类,可以直接用JObject做灵活解析:

1. 安装依赖包

在NuGet包管理器中搜索并安装Newtonsoft.Json。

2. 修改代码解析

Imports Newtonsoft.Json.Linq ' 记得添加命名空间

' ... 原代码逻辑 ...
Dim returnValue As String = result.Content.ReadAsStringAsync().Result
If result.StatusCode <> 200 Then
    Throw New Exception($"Failed to POST data: ({result.StatusCode}): {returnValue}")
Else
    Debug.Print(returnValue)
    ' 直接解析JSON对象
    Dim jsonObj = JObject.Parse(returnValue)
    Dim sadadNumber = jsonObj("data")("sadadNumber").ToString()
    Debug.Print($"提取到的sadadNumber: {sadadNumber}")
End If

注意事项

  • 你的响应字符串显示有转义符(\"),但实际通过HTTP客户端获取returnValue时,框架会自动处理转义,无需手动清理即可直接解析。
  • 若使用低版本.NET Framework,优先选择Newtonsoft.Json,因为System.Text.Json对旧版本框架支持有限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 19:55:14