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

使用VB.NET反序列化多层JSON字符串时无数据填充问题排查

问题原因及解决方法

核心错误

你定义的类结构和JSON的层级、字段完全不匹配,序列化库无法找到对应的映射关系,因此不会抛出异常但所有属性都保持默认空值:

  • JSON最外层是一个对象,它包含一个名为devices的单个对象属性,该属性才是携带totalCount、pageNum、transactions的结构
  • 你定义的devices类中多余添加了devicelist As List(Of device)属性,和JSON实际结构完全不符,导致映射失败

修正后的类定义

' 根类,对应JSON最外层结构
Public Class RootResponse
    Public Property devices As DeviceSummary
End Class

' 对应JSON中devices字段的结构
Public Class DeviceSummary
    Public Property pageNum As Integer
    Public Property totalCount As Integer
    Public Property totalPage As Integer
    Public Property transactions As List(Of Transaction)
End Class

Public Class Transaction
    Public Property transactionId As String
    Public Property state As String
    Public Property type As String
    Public Property operationType As String
    Public Property devices As List(Of MyDevice)
End Class

Public Class MyDevice
    Public Property imei As String
    Public Property code As Integer
    Public Property message As String
    Public Property data As String
End Class

反序列化代码示例

使用Newtonsoft.Json(推荐,VB.NET生态最常用)

先通过NuGet安装Newtonsoft.Json包,再执行反序列化:

Imports Newtonsoft.Json

' responseContent为你获取到的接口响应字符串
Dim result As RootResponse = JsonConvert.DeserializeObject(Of RootResponse)(responseContent)

使用.NET内置System.Text.Json

适用于.NET Core/.NET 5+版本,需要开启不区分大小写匹配避免映射失败:

Imports System.Text.Json

Dim serializeOptions As New JsonSerializerOptions With {
    .PropertyNameCaseInsensitive = True
}
Dim result As RootResponse = JsonSerializer.Deserialize(Of RootResponse)(responseContent, serializeOptions)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 05:27:03