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

VB.NET中NewtonSoft.JSON反序列化异常:顶层报错/深层返回null

问题根因
  • 层级结构不匹配:你在smsdata类中定义的messages属性类型为自定义messages类,额外多嵌套了一层结构。实际JSON中data节点下的messages直接是消息数组,不需要再包裹一层带messages属性的对象,这是反序列化后字段全为null的核心原因。
  • 类型不匹配:JSON中http_code、total_count、date、user_id等字段为数值类型,实体类中统一声明为String,在严格反序列化配置下会触发报错或赋值失败。
修复方案
  1. 删除冗余的messages自定义类,直接将smsdata中的messages属性定义为List(Of message)类型
  2. 修正实体类属性的类型,与JSON字段类型匹配,避免隐式转换失败
  3. 可统一为实体类属性添加JsonProperty注解,避免命名规则、大小写差异导致的映射异常

以下是修改后可正常运行的完整代码:

Imports Newtonsoft.Json

Public Class response
    <JsonProperty("http_code")>
    Public Property http_code As Integer
    <JsonProperty("response_code")>
    Public Property response_code As String
    <JsonProperty("response_msg")>
    Public Property response_msg As String
    <JsonProperty("data")>
    Public Property data As smsdata
End Class

Public Class smsdata
    <JsonProperty("total_price")>
    Public Property total_price As Decimal
    <JsonProperty("total_count")>
    Public Property total_count As Integer
    <JsonProperty("queued_count")>
    Public Property queued_count As Integer
    <JsonProperty("messages")>
    Public Property messages As List(Of message)
End Class

Public Class message
    <JsonProperty("direction")>
    Public Property direction As String
    <JsonProperty("date")>
    Public Property daterange As Long
    <JsonProperty("to")>
    Public Property recipient As String
    <JsonProperty("body")>
    Public Property body As String
    <JsonProperty("from")>
    Public Property from As String
    <JsonProperty("schedule")>
    Public Property schedule As Long
    <JsonProperty("message_id")>
    Public Property message_id As String
    <JsonProperty("message_parts")>
    Public Property message_parts As Integer
    <JsonProperty("message_price")>
    Public Property message_price As String
    <JsonProperty("from_email")>
    Public Property from_email As String
    <JsonProperty("list_id")>
    Public Property list_id As String
    <JsonProperty("custom_string")>
    Public Property custom_string As String
    <JsonProperty("contact_id")>
    Public Property contact_id As String
    <JsonProperty("user_id")>
    Public Property user_id As Integer
    <JsonProperty("subaccount_id")>
    Public Property subaccount_id As Integer
    <JsonProperty("country")>
    Public Property country As String
    <JsonProperty("carrier")>
    Public Property carrier As String
    <JsonProperty("status")>
    Public Property status As String
End Class

Module Program
    Sub Main(args As String())
        Dim jsonstring As String = "{'http_code': 200, 'response_code': 'SUCCESS', 'response_msg': 'Messages queued for delivery.', 'data': { 'total_price': 0.0516, 'total_count': 2, 'queued_count': 2, 'messages': [ { 'direction': 'out', 'date': 1637744634, 'to': '+44771111111111', 'body': 'test message, please ignore 1', 'from': 'Shield', 'schedule': 1637744634, 'message_id': 'A3F48CA8-79E6-416F-A8C6-7660BD7B7632', 'message_parts': 1, 'message_price': '0.0258', 'from_email': null, 'list_id': null, 'custom_string': '', 'contact_id': null, 'user_id': 100525, 'subaccount_id': 116285, 'country': 'GB', 'carrier': 'O2', 'status': 'SUCCESS' }, { 'direction': 'out', 'date': 1637744634, 'to': '+4477222222222', 'body': 'test message, please ignore 2', 'from': 'Clubcare', 'schedule': 1637744634, 'message_id': '670E96E2-ED4F-4FAB-8594-D49B244EA2C3', 'message_parts': 1, 'message_price': '0.0258', 'from_email': null, 'list_id': null, 'custom_string': '', 'contact_id': null, 'user_id': 100525, 'subaccount_id': 116285, 'country': 'GB', 'carrier': 'O2', 'status': 'SUCCESS' } ], '_currency': { 'currency_name_short': 'GBP', 'currency_prefix_d': '£', 'currency_prefix_c': 'p', 'currency_name_long': 'British Pounds' } } }"
        Dim results = JsonConvert.DeserializeObject(Of response)(jsonstring)
        ' 此时results内所有字段均可正常取值
    End Sub
End Module

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 08:24:02