Rohith Nair
Rohith Nair

Reputation: 1070

Converting JSON data to VB.NET multidimensional array

I have a multidimensional array converted to JSON data in this format.

"[[null,null,null,null,null,null],[null,null,null,1,1,null],[null,null,null,null,1,1],[null,null,null,null,null,null],[null,null,null,null,null,null]]"

I am trying to conver this multidimensional array of strings/integers to equivalent form using JavascriptSerializer like this

 Dim retValue As List(Of String)
 Dim deserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()

retValue deserializer.Deserialize(Of List(Of String))(o.value) 

Its throwing an exception: Type 'System.String' is not supported for deserialization of an array.

I tried the same casting it to Integers , but the same exception occured.

How can I perform the conversion using .NET 3.5.

I dont want to use JSON.NET dll's , if System.Web.Script.Serialization.JavaScriptSerializer can do the job.

Any suggestions?

Upvotes: 2

Views: 4050

Answers (2)

Rohith Nair
Rohith Nair

Reputation: 1070

I tried two ways in achieving the solution:-

As per "SeanCocteau's comments:-

 Dim o As Object = JSONField
 Dim castObj() As Object = Nothing
 Dim ser As New DataContractJsonSerializer(GetType(System.Object))

 Using ms As New MemoryStream(System.Text.Encoding.UTF8.GetBytes(o.Value().ToString))
   castObj = ser.ReadObject(ms)
 End Using

 Dim convObject As ArrayList = New ArrayList(castObj)

In this format, I was able to get the arrayList and then take each internal arrayList by iterating and casting appropriately

Second solution I tried:-

    Dim o As Object =  JSONField
    Dim obj() As Object = (New JavaScriptSerializer()).Deserialize(Of Object)(o.Value.ToString())
    Dim convObject As ArrayList = New ArrayList(obj)

In this format also , I was able to get the arrayList and then take each internal arrayList by iterating and casting appropriately

Thanks SeanCocteau for giving the tip to Convert it to Object first :)

Upvotes: 0

SeanCocteau
SeanCocteau

Reputation: 1876

You're attempting to convert that Json into a one dimensional List of Strings when in fact it is a Jagged Array of Strings. With Generics I don't believe you can smartly nest other strongly typed objects (although I welcome corrections to this) and you cannot easily convert them across (unless you had a rigid structure in which case a POCO with DataContracts would be the way forward).

My approach would be to serialise into an Object, where Strings, Integers and Arrays can co-exist happily; they simply need boxing out as appropriate. Not ideal I admit. I haven't tried the JavaScriptSerializer but have tested and confirmed the following works using DataContractJsonSerialiser...

string json = "[[null,null,null,null,null,null],[null,null,null,1,1,null],[null,null,null,null,1,1],[null,null,null,null,null,null],[null,null,null,null,null,null]]";
            object castObj = null;
            DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof( System.Object ));

            // Create dummy stream to read into
            using (MemoryStream ms = new MemoryStream( System.Text.Encoding.UTF8.GetBytes( json ) ) ){
                castObj = ser.ReadObject(ms);
            }

HTH

Upvotes: 2

Related Questions