【问题标题】:Json Deserialize Nested Objects in Vb (Newtonsoft)Json反序列化Vb中的嵌套对象(Newtonsoft)
【发布时间】:2019-12-20 10:51:09
【问题描述】:

基本上,我正在尝试从 API 获取数据。

我的 JSON 是:

[
  {
    "id": 101,
    "items": [
      {
        "id": 91,
        "quantity": 141,
        "size": "12",
        "assigned": false,
        "item": {
          "pk": 27,
          "title": "test title",
          "description": "test description",
          "designer": "designer",
          "category": 4,
          "size": "12,14,16",
          "image": "media/products/indir.jpg",
          "price": 31.0,
          "resin_gr": 31.0
        }
      },
      {
        "id": 92,
        "quantity": 18,
        "size": "14",
        "assigned": false,
        "item": {
          "pk": 26,
          "title": "Bileklik 5",
          "description": "bileklik",
          "designer": "designer",
          "category": 2,
          "size": "12,14,16",
          "image": "media/products/NB00316.png",
          "price": 50.0,
          "resin_gr": 14.0
        }
      }
    ]
  }
]

我的课程是:

 Public Class Orders
        Public Property id As Integer
        Public Property items As IEnumerable(Of OrderItems)
    End Class

    Public Class OrderItems
        Public Property id As Integer
        Public Property quantity As String
        Public Property size As String
        Public Property assigned As String
        Public Property item As List(Of OrderProduct)
    End Class

    Public Class OrderProduct
        Public Property pk As Integer
        Public Property title As String
        Public Property description As String
        Public Property designer As String
        Public Property category As String
        Public Property size As String
        Public Property image As String
        Public Property price As String
        Public Property resin_gr As String
    End Class

我正在尝试像这样解析 JSON:

Dim result As IEnumerable(Of Orders) = JsonConvert.DeserializeObject(Of IEnumerable(Of Orders))(JSONDATA)

我在路径“[0].items[0].item.pk”,第 1 行,位置 86 上遇到错误。

我尝试了什么:

我将 orders.item 更改为 list 但没有任何改变。

我该怎么办?

【问题讨论】:

  • 你遇到了什么错误?
  • 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.IEnumerable`1[MegaPcSlicer.NovaHTTP+OrderProduct]' 因为该类型需要正确反序列化的 JSON 数组(例如 [1,2,3])。路径 '[0].items[0].item.pk',第 1 行,位置 86。
  • 错误明确指出它需要一个 JSON 数组,但您的文档将项目作为对象:"item": {
  • @AFriend 我实际上尝试将 "item" : { 更改为 "item" : [{..... 但仍然不起作用。
  • 两个订单项?错误有变化吗?

标签: json vb.net json.net


【解决方案1】:
Public Class Orders
    Public ID As Integer
    Public Items As IEnumerable(Of OrderItems)
End Class
Public Class OrderItems
    Public ID As Integer
    Public quantity As Integer
    Public size As Integer
    Public assigned As Boolean
    Public Item As OrderProduct
End Class
Public Class OrderProduct
    Public pk As Integer
    Public title As String
    Public description As String
    Public designer As String
    Public category As Integer
    Public size As String
    Public image As String
    Public price As Double
    Public resin_gr As Double
End Class

编辑:就像一位朋友评论的那样,这很有效,因为您试图在此处将对象解析为列表:

Public Property item As List(Of OrderProduct)

当它需要时

Public Item As OrderProduct

当我为指定的 JSON 重新创建自定义对象时,不知何故我错过了这个问题并解决了这个问题。

【讨论】:

  • 这是个糟糕的建议。您的答案有效的真正原因是您将 List(Of OrderProduct) 更改为 OrderProduct 以匹配他们的 JSON。
  • 我同意“朋友”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
相关资源
最近更新 更多