1

The code I have to work with is Visual Basic. I have JSON that needs to be deserialized to an object.

I am running into the error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type System.Collections.Generic.List1[AgentGroup] because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '[0].agentGroup.id', line 3, position 7

Now what I've done is instantiated the object, serialized it and write to a text file so that I can see how that data looks in JSON, which came out to this:

{
  "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
      }
    }
  ]
}

But what the JSON I need to deserialize is in this format:

[{
    "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
        }
    }
}]

I believe it has to do with the beginning square bracket [, I have searched on how to address this issue but being that the code is in Visual Basic, I have not found anything that helps. My code is:

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)

But this is where the error occurs. I also see that when I write it to the text file my JSON looks different from what I need to deserialize. I am not sure what can be causing this

1
  • Did my answer work for you? Commented Oct 16, 2018 at 18:05

2 Answers 2

1

This sounds a bit simplistic but can you do the following (done with VBA as per tag - oops) ?

Standard module:

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

And use the class CJSON from here

Code to empty (might need some tidying up - depends if on right lines):

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:

Output

Sign up to request clarification or add additional context in comments.

5 Comments

Never heard of CJSON before. Though 1800 lines of code just to parse some JSON seems a bit too much since it can be done with 150 lines or so with no external dependencies.
@FlorentB. It was a bit of a punt on my part. I would love to know how to do it properly if you could point me in the right direction please?
those are 3 different examples of implementation: JsonRe (light parser based on regex), JsonTK (light parser), JsonIO (full featured serializer).
@FlorentB. Many thanks!! Is there a README or some such to go with these?
No read me. The first two contain a single function Parse taking a string and returning either a base1 Array or a Collection which can be changed to a Dictionary.
0

So I figured out what was causing the problem. The JSON:

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

I already had a VB class called agentField which consisted of 5 properties:

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

Then another class called AgentGroup:

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

So what I ended up doing was creating a new class to contain the AgentGroup Class:

Public Class Container
    Public Property agentGroup() As AgentGroup

Then all I needed to do was create a List of Container:

Dim agentGroupList As New List(Of Container)

So that it can hold all the AgentGroup objects that come in the JSON and when I Deserialize it, it worked perfectly. Since AgentField was used in AgentGroup it as a property the deserialization worked perfectly, all I was missing was a class to contain the AgentGroup class and then create a List to hold all the objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.