skip to Main Content

Getting the following error

Newtonsoft.Json.JsonSerializationException:
‘Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type ‘System.Collections.Generic.List`1[]’ 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.

I have it working already on a loop with multiple selectToken’s, but its kind of slow calling 50 odd of those. I’d like to speed it up.

I’m going to omit some of the value names since its quite long.

But lets say I have nested classes

edit: this is with the Newtonsoft JSON library

Public Class Orders
    Public recordSetTotal As Integer

    Public Class ordersList 'orders list is an array in the JSON data source
        Public Class orderDetails
            Public grandTotal As Single
            Public placedDate As String
        End Class
    
        Public Class address
            Public postcode As String
            Public addressLine() As String 'this is an array in the JSON data source
        End Class
    End Class
End Class
'orderData is the JSON string block
Dim ordersList As List(Of Orders) = JsonConvert.DeserializeObject(Of List(Of Orders))(orderData)

orderData might look like this (I’ve snipped the json up for an example so may be error in the json, but shouldn’t be in the source). I’ve only put 1 array entry for ordersList but it will have multiple and match recordSetTotal.

{
   "recordSetTotal":10,
   "ordersList":[
      {
         "orderDetails":{
            "grandTotal":50.0000,
            "placedDate":"2022-12-25 12:00:00.000"
         },
         "addressLine":[
            "123 Fake St",
            "Somewhere",
            "ABG"
         ]
      }
   ]
}

Would it better to break out some of the nesting so I can make ordersList an array?

Public Class Orders
    Public recordSetTotal As Integer
    Public ordersList() as ordersList
End Class

Public Class ordersList 'orders list is an array in the JSON data source
    Public Class orderDetails
        Public grandTotal As Single
        Public placedDate As String
    End Class
    
    Public Class address
        Public postcode As String
        Public addressLine() As String 'this is an array in the JSON data source
    End Class
End Class

And If I was going to de-nest the classes, should the array be like this:

Public ordersList() as ordersList

or this:

Public ordersList as ordersList()

2

Answers


  1. Chosen as BEST ANSWER

    So in case anyone else was wondering, check the commennt left by GSerg on my post.

    All the classes have to be un-nested. And everything inside the class must be a public property, including objects from outside classes.

    An array must be public property ordersList as List(of ordersList) for example rather than public property ordersList() as ordersList

    Than reading the json is as simple as Dim deserialised As Orders = JsonConvert.DeserializeObject(Of Orders)(json)

    Any json keys/fields that dont have a matching name as a public property wont be read into the results obviously.


  2. you need to add a root class, your classes should be

     Public Class Root
            Public Property recordSetTotal As Integer
            Public Property ordersList As OrdersList()
        End Class
    
      Public Class OrdersList
            Public Property orderDetails As OrderDetails
            Public Property addressLine As String()
        End Class
    
    Public Class OrderDetails
            Public Property grandTotal As Double
            Public Property placedDate As String
        End Class
    

    and code

    Dim ordersList As List(Of Orders) = JsonConvert
                                       .DeserializeObject(Of Root)(orderData).ordersList 
      
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search