2

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?

Rohith Nair
  • 1,080
  • 1
  • 17
  • 33
  • [null,null,null,1,1,null] -> 1 is not a string,should be [null,null,null,"1","1",null] – Myra Feb 27 '12 at 12:45
  • @Myra ok but I tried the conversions in Integer also , but the result was same, exception message just changed to Integer not supported. Thanks – Rohith Nair Feb 27 '12 at 12:59
  • Have you considered the DataContractJsonSerialiser (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx)? I would also be tempted to specifically apply a DataContract to the object (rather than a list of Strings) for serialising back and forth... – SeanCocteau Feb 27 '12 at 13:35
  • @SeanCocteau you mean to say ,I should create Data Contract class and convert to that class object for doing this? That will be a lot of unwanted work ,right? I just want my two dimensional array [By the converted format, I suppose it's jagged array] to equivalent VB.NET – Rohith Nair Feb 27 '12 at 14:54
  • Sorry I assumed you were dealing with specific object - hence the data contracts. Anyhoo - answer provided below... – SeanCocteau Feb 27 '12 at 15:56

2 Answers2

2

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

SeanCocteau
  • 1,838
  • 19
  • 25
  • Thanks it helped. I then converted the Object to ArrayList and then taking each ArrayList from inside the ArrayList using indexes. I am looking for a crisp one. The catch is I know the type of Object I am getting back at the server. – Rohith Nair Feb 28 '12 at 12:41
  • Tbh - If you know the kind of object you are getting back, why not utilise a DTO (Data Transfer Object) to serialise to and deserialise from? This would certainly save you the boxing hassle... – SeanCocteau Feb 28 '12 at 14:09
  • they are actually integers.[ A matrix representation containing either 0/1 in columns]. I was hoping to directly deserialise and type cast using Deserialize .But then I followed your suggestions – Rohith Nair Feb 28 '12 at 14:40
0

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 :)

Rohith Nair
  • 1,080
  • 1
  • 17
  • 33