0

Below is my json text

[{
    "Type": "Type Value",
    "Name": "Name Value",   
    "Elements": {"Element1": {"one":"1", "two":"2"}, "Element2": {"three":"3", "four":"4"}}
}]

and below is my class

Public Class TestJson
    Public Property Property1 As Class2()
End Class

Public Class Class2
    Public Property Type As String
    Public Property Name As String
    Public Property Elements As Elements
End Class

Public Class Elements
    Public Property Element1 As Element1
    Public Property Element2 As Element2
End Class

Public Class Element1
    Public Property one As String
    Public Property two As String
End Class

Public Class Element2
    Public Property three As String
    Public Property four As String
End Class

Below deserialise using newtownsoft is throwing exception

Dim classObj = JsonConvert.DeserializeObject(Of TestJson)(JsonText)

Below is the exception Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

Chembu
  • 9
  • 2
  • 1
    Does this answer your question? [Cannot deserialize the current JSON array (e.g. \[1,2,3\]) into type](https://stackoverflow.com/questions/17762032/cannot-deserialize-the-current-json-array-e-g-1-2-3-into-type) – Charlieface Aug 24 '22 at 05:33

1 Answers1

0

As per the error message, you're deserialize a collection of TestJson to a single instance. Update your Deserialization call like this and see how it goes

Dim classObj = JsonConvert.DeserializeObject(Of List(Of TestJson))(testJson)
Hursey
  • 541
  • 2
  • 8
  • 17