无法将当前JSON对象反序列化为类型

时间:2018-04-04 14:59:58

标签: json vb.net

我必须使用的代码是Visual Basic。我有需要反序列化为对象的JSON。

我遇到了错误: 无法将当前JSON对象(例如{"name":"value"})反序列化为类型System.Collections.Generic.List1[AgentGroup],因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似于数组的集合类型或者List)可以从JSON对象反序列化。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。 路径'[0] .agentGroup.id',第3行,第7位

现在我所做的是实例化对象,将其序列化并写入文本文件,以便我可以看到数据在JSON中的外观,其中包含:

{
  "agentGroup": [
    {
      "Id": 6873450,
      "GroupName": "sig_latam_buenosaires",
      "Fields": {
        "Organization": "Football_Inc",
        "LoadBalanced": "No",
        "Description": "bye",
        "TransferConcurrency": "",
        "IsMxEnabled": false
      }
    },
    {
      "Id": 6873450,
      "GroupName": "latam_buenosaires",
      "Fields": {
        "Organization": null,
        "LoadBalanced": null,
        "Description": null,
        "TransferConcurrency": null,
        "IsMxEnabled": false
      }
    },
    {
      "Id": 666,
      "GroupName": "NY",
      "Fields": {
        "Organization": "TechniColor",
        "LoadBalanced": "Yes",
        "Description": "Hello World",
        "TransferConcurrency": "",
        "IsMxEnabled": true
      }
    }
  ]
}

但我需要反序列化的JSON是这种格式:

[{
    "agentGroup": {
        "id": 9943652,
        "groupName": "technicolorBangalore",
        "fields": {
            "organization": "TechniColor",
            "loadBalanced": "Yes",
            "description": "Technicolor Bangalore Agents",
            "mxEnabled": false,
            "transferConcurrency": null
        }
    }
}, {
    "agentGroup": {
        "id": 6873450,
        "groupName": "sig_latam_buenosaires",
        "fields": {
            "organization": "Viacom_Inc",
            "loadBalanced": "No",
            "description": "",
            "mxEnabled": false,
            "transferConcurrency": null
        }
    }
}]

我认为它与开始的方括号[有关,我已经搜索了如何解决这个问题,但由于代码是在Visual Basic中,我没有发现任何有用的东西。我的代码是:

Dim reader As New 
   StreamReader("C:\Users\poncek\Desktop\SigniantTextFile\AgentGroupList.txt")
   Dim jsonString as String = reader.ReadToEnd
   Dim works = JsonConvert.DeserializeObject(Of List(Of AgentGroupList))(jsonString)

但这是错误发生的地方。我还看到,当我将它写入文本文件时,我的JSON看起来与我需要反序列化的不同。我不确定是什么导致这个

2 个答案:

答案 0 :(得分:1)

这听起来有点简单,但你可以做以下事情(根据标签完成VBA - oops)?

标准模块:

Public Sub Example()

 Dim JSONString As String
 JSONString = Range("A1").Text

 Dim JSON As cJSON
 Set JSON = New cJSON

 Dim D As Dictionary
 Set D = JSON.Deserialize(JSONString)

End Sub

并使用here

中的CJSON类

代码清空(可能需要整理一下 - 取决于是否在右边):

Public Sub Example()

    Dim JSONString As String
    JSONString = Range("A1").Text 'This holds the original JSON string you provided

    Dim JSON As cJSON
    Set JSON = New cJSON

    Dim D As Dictionary
    Set D = JSON.Deserialize(JSONString)

    Dim key As Variant
    Dim key2 As Variant
    Dim key3 As Variant

    For Each key In D.Keys

        For Each key2 In D(key).Keys

            For Each key3 In D(key)(key2)

                Select Case TypeName(D(key)(key2)(key3))

                Case "Long", "String"

                   Debug.Print key, key1, key2, key3, D(key)(key2)(key3)

                Case "Dictionary"

                    Dim key4 As Variant

                     For Each key4 In D(key)(key2)(key3).Keys
                         Debug.Print key, key1, key2, key3, key4, D(key)(key2)(key3)(key4)
                     Next key4

                End Select

            Next key3

        Next key2

    Next key

End Sub

输出:

Output

答案 1 :(得分:0)

所以我弄清楚导致问题的原因。 JSON:

 "agentGroup": {
        "id": 6873450,
        "groupName": "sig_latam_buenosaires",
        "fields": {
            "organization": "Viacom_Inc",
            "loadBalanced": "No",
            "description": "",
            "mxEnabled": false,
            "transferConcurrency": null
        }

我已经有一个名为agentField的VB类,它包含5个属性:

Public Class AgentFields
     Public Property Organization() As String
     Public Property LoadBalanced() As String
     Public Property Description() As String
     Public Property TransferConcurrency() As String
     Public Property IsMxEnabled() As Boolean

然后是另一个名为AgentGroup的类:

  Public Class AgentGroup
        Public Property Id() As Integer
        Public Property GroupName() As String
        Public Property Fields() As AgentFields

所以我最终要做的是创建一个包含AgentGroup类的新类:

Public Class Container
    Public Property agentGroup() As AgentGroup

然后我需要做的就是创建一个容器列表:

Dim agentGroupList As New List(Of Container)

因此它可以保存JSON中的所有AgentGroup对象,当我反序列化它时,它完美地工作。由于AgentField在AgentGroup中使用,它作为一个属性,反序列化工作完美,我所缺少的是一个包含AgentGroup类的类,然后创建一个List来保存所有对象。