【问题标题】:Convert JSON array objects to .net Classes将 JSON 数组对象转换为 .net 类
【发布时间】:2019-08-15 16:35:43
【问题描述】:

我在将以下 JSON 字符串转换为 .net 类时遇到问题。到目前为止,我创建的课程也在下面。我无法开始工作的特定部分是 group_access 和角色标签。我知道这些是对象数组,但是当我尝试转换对象时,出现以下错误;无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Array”,因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。

我还尝试使用 Visual Studio 工具(Edit>Paste Special > JSON)和 jscon2csharp.com,它在转换角色和 group_access 标签时出错。

JSON 数据:

    {
    id: 122404,
    email: 'mike@email.com',
    fname: 'Mike',
    lname: 'Doe',
    full_name: 'Mike Doe',
    resource_id: '001002',
    title: '',
    last_login: '2016-11-01 09:15:23',
    tags: [
    'Math Department', 'New Teachers'
    ],
    grades: [
    4, 5, 6
    ],
    targets: [
    'Option 1: 3 Informal Obs.'
    ],
    caseload_tags: [],
    group_access: [
    10: 2
    25527: 1
    25645: 1
    25653: 4
    ],
    roles: [
    10: [
        2015: 2,
        2016: 2
    ],
    25527: [
        2015: 2,
        2016: 1
    ]
    25645: [
        2015: 1,
        2016: 1
    ]
    25653: [
        2015: 3,
        2016: 4
    ]
    ]
    }

我的代码:

    Public Class Item
      Public Property id As Integer
      Public Property email As String
      Public Property fname As String
      Public Property lname As String
      Public Property full_name As String
      Public Property resource_id As String
      Public Property title As String
      Public Property tags As IList(Of String)
      Public Property grades As IList(Of String)
      Public Property targets As IList(Of String)
      Public Property group_access As Array
      Public Property roles As Array
    End Class

    Public Class RootObject
      Public Property type As String
      Public Property limit As Integer
      Public Property offset As Integer
      Public Property total As Integer
      Public Property time As String
      Public Property items As List(Of Item)
      Public Property items_count As Integer 
    End Class

【问题讨论】:

    标签: arrays .net json vb.net


    【解决方案1】:

    JSON 无效 - 数组元素必须用逗号分隔。

    【讨论】:

    • 我无法控制如何将数据呈现给我。这就是它来自我正在连接的应用程序的方式。
    • 你得到了无效的 json,你不能期望使用 json 库来处理它。
    猜你喜欢
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2012-07-21
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多