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?

5
  • [null,null,null,1,1,null] -> 1 is not a string,should be [null,null,null,"1","1",null] Commented Feb 27, 2012 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 Commented Feb 27, 2012 at 12:59
  • Have you considered the DataContractJsonSerialiser (msdn.microsoft.com/en-us/library/…)? I would also be tempted to specifically apply a DataContract to the object (rather than a list of Strings) for serialising back and forth... Commented Feb 27, 2012 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 Commented Feb 27, 2012 at 14:54
  • Sorry I assumed you were dealing with specific object - hence the data contracts. Anyhoo - answer provided below... Commented Feb 27, 2012 at 15:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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

1 Comment

@JoeCoderGuy JSONField contains a json style object [[a,b,c,d,e,f],[g,h,i,j,k,l]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.