【发布时间】:2025-12-12 08:25:01
【问题描述】:
我正在将 JSON 从我的 Angular 前端(带有 HttpClient 帖子)发送到我的 VB.NET 后端,但我似乎无法正确反序列化它。
JSON
{
"taxRate":0,
"shipping":0,
"items":
[
{
"id":392184,
"name":"",
"price":0,
"image":"",
"quantity":1,
"data":"sku1"
},
{
"id":338429,
"name":"",
"price":0,
"image":"",
"quantity":1,
"data":"sku2"
},
{
"id":386646,
"name":"",
"price":0,
"image":"",
"quantity":1,
"data":"sku3"
}
]
}
我的课
Public Class CartDTO
Public Property Shipping As Decimal
Public Property TaxRate As Decimal
Public Property Items() As CartItemDTO
End Class
Public Class CartItemDTO
Public Property Id As Integer
Public Property Name As String
Public Property Image As String
Public Property Price As Decimal
Public Property Quantity As Integer
Public Property Data As String
End Class
捕捉这个的控制器函数:
Public Sub PostValue(<FromBody()> ByVal value As Object)
'breakpoint
End Sub
如果我输入value As String,那么value 为空。
如果 value As CartDTO 则 value 是一个空的 CartDTO。
如果value As Object 则value 是Newtonsoft.Json.Linq.JObject 类型,它具有正确的数据,但我不知道如何轻松将其放入我的DTO...
试试这个:
Dim ob = JsonConvert.DeserializeObject(Of CartDTO)(value)
...给我 System.InvalidCastException: '从类型 'JObject' 转换为类型 'String' 无效。'
我错过了什么?
编辑 - 或者我怎样才能将 json 作为字符串接收?不知道为什么 value 在我尝试时什么都没有。
【问题讨论】: