NewtonSoft.JSON 反序列化 - 未正确反序列化。 (VB.NET)
Posted
技术标签:
【中文标题】NewtonSoft.JSON 反序列化 - 未正确反序列化。 (VB.NET)【英文标题】:NewtonSoft.JSON Deserialize - Not deserializing correctly. (VB.NET) 【发布时间】:2022-01-02 18:52:15 【问题描述】:我正在尝试反序列化 JSON 返回,但是当我尝试在***别反序列化时它会出错,或者如果我开始更深入地了解 json,它不会出错并且只返回空值。
我的其余反序列化只得到“null”返回 - 我已经成功地分别反序列化了 messages
,但是当我尝试做所有事情时,它只会将空值返回给我的所有值。
当我尝试在response
反序列化时,它提供了错误,我不清楚究竟是什么错误。
型号
Public Class response
public property http_code as string
public property response_code as string
public property response_msg as string
public property data as smsdata
End Class
Public Class smsdata
public property total_price as string
public property total_count as string
public property queued_count as string
public property messages as messages
End Class
Public Class messages
Public property messages as list(of message)
End Class
Public Class message
Public Property direction as string
<JsonProperty("date")>
Public property daterange as string
<JsonProperty("to")>
public property recipient as string
public property body as string
public property from as string
public property schedule as string
public property message_id as string
public property message_parts as string
public property message_price as string
public property from_email as string
public property list_id as string
public property custom_string as string
public property contact_id as string
public property user_id as string
public property subaccount_id as string
public property country as string
public property carrier as string
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)
End Sub
End Module
【问题讨论】:
@dba 单括号有效 - 我有一个较早的示例,我反序列化了“消息”元素。当我扩大规模时,这失败了。示例单括号。 newtonsoft.com/json/help/html/deserializeobject.htm 【参考方案1】:检查您的代码和示例数据:
Public Class smsdata
public property total_price as string
public property total_count as string
public property queued_count as string
public property messages as List (Of message)
End Class
应该做的伎俩,你可以转储消息类
(从单个 qoutes 开始:我从未使用过它们,但是 VS-Code 将它们标记为错误,但是 :-))
【讨论】:
这行得通——单引号也行得通。以上是关于NewtonSoft.JSON 反序列化 - 未正确反序列化。 (VB.NET)的主要内容,如果未能解决你的问题,请参考以下文章
NewtonSoft.JSON 反序列化 - 未正确反序列化。 (VB.NET)