I have a web service in vb.net which returns json-formatted data. One of the data items can take a number of different types of values: Boolean
, String
, Dictionary(of String, String)
or Dictionary(Of String, Object)
The latter is to allow flexible lists of data to be returned. Each then has an itemType and DataType specified in the response so the thirtd party knows what to expect. This has worked fine.
However, I am now getting the following error trying to return a Dictionary(Of String, Dictionary(Of String, List(Of String)))
:
Value of type 'System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String)))' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, Object)'.
It is quite happen to take a Dictionary(Of String, Dictionary(Of String, String))
but not a Dictionary(Of String, Dictionary(Of String, List(Of String)))
. I am very confused - I though pretty much anything could convert to Object
? Why can a Dictionary(Of String, String)
convert to oject but not a Dictionary(Of String, List(Of String))
?
I can get round it by doing the following:
Dim Bar As New Dictionary(Of String, Dictionary(Of String, List(Of String)))
' Add stuff to bar here
Dim Foo As New Dictionary(Of String, Object)
For Each Row As KeyValuePair(Of String, Dictionary(Of String, List(Of String))) In Bar
Foo.Add(Row.Key, New Dictionary(Of String, Object))
For Each Item As KeyValuePair(Of String, List(Of String)) In Row.Value
Foo(Row.Key).add(Item.Key, Item.Value)
Next
Next
but I don't understand why I need to. is there something I am missing that could cause a problem later and can anyone explain what sorts of objects cannot be cast to Object
?