Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Using the code below,
C#
HttpContext context.Response.ContentType = "application/json";
var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var obj = javaScriptSerializer.DeserializeObject(stream);
object[] arr = (object[])obj;

The result of arr obtained. The arr is a 3-level array object. E.g., below is the displayed in Watch panel:
Name   Value            Type
arr[2] {object[3]       object{object[]}
   [0] {object[2]       object{object[]}
   [1] {object[2]       object{object[]}
   [2] {object[2]       object{object[]}
      [0] "6512"        object{string}
      [1] "In Invert"   object{string}

I tried to get retrieve the values of the elements, e.g. arr[2][2][2], but failed.
Then, from Immediate Window, I retrieved arr[2] (see below).
C#
arr[2].GetType()
{Name = "Object[]" FullName = "System.Object[]"}
    [System.RuntimeType]: {Name = "Object[]" FullName = "System.Object[]"}
    base {System.Reflection.MemberInfo}: {Name = "Object[]" FullName = "System.Object[]"}
    Assembly: {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
    AssemblyQualifiedName: "System.Object[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    Attributes: Public | Sealed | Serializable
    BaseType: {Name = "Array" FullName = "System.Array"}
    ContainsGenericParameters: false
    DeclaringMethod: 'obj.GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
    DeclaringType: null
    FullName: "System.Object[]"
    GenericParameterAttributes: 'obj.GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
    GenericParameterPosition: 'obj.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
    GenericTypeArguments: {System.Type[0]}
    GUID: {00000000-0000-0000-0000-000000000000}
    HasElementType: true
    IsAbstract: false
    IsAnsiClass: true
    IsArray: true
    IsAutoClass: false
    IsAutoLayout: true
    IsByRef: false
    IsClass: true
    IsCOMObject: false
    IsConstructedGenericType: false
    IsContextful: false
    IsEnum: false
    IsExplicitLayout: false
    IsGenericParameter: false
    IsGenericType: false
    IsGenericTypeDefinition: false
    IsImport: false
    IsInterface: false
    IsLayoutSequential: false
    IsMarshalByRef: false
    IsNested: false
    IsNestedAssembly: false
    IsNestedFamANDAssem: false
    IsNestedFamily: false
    IsNestedFamORAssem: false
    IsNestedPrivate: false
    IsNestedPublic: false
    IsNotPublic: false
    IsPointer: false
    IsPrimitive: false
    IsPublic: true
    IsSealed: true
    IsSecurityCritical: false
    IsSecuritySafeCritical: false
    IsSecurityTransparent: true
    IsSerializable: true
    IsSpecialName: false
    IsUnicodeClass: false
    IsValueType: false
    IsVisible: true
    MemberType: TypeInfo
    Module: {CommonLanguageRuntimeLibrary}
    Namespace: "System"
    ReflectedType: null
    StructLayoutAttribute: null
    TypeHandle: {System.RuntimeTypeHandle}
    TypeInitializer: null
    UnderlyingSystemType: {Name = "Object[]" FullName = "System.Object[]"}

Since its IsEnm property is false, I don't know how I can get the sub-objects: arr[2][0] and arr[2][1].
I don't know if there is method to convert object[] to object so I can retrieve the sub-objects.
Hopefully, someone can help for this issue. Thanks.

What I have tried:

How to Deserialize object and retrieve the elements' values?
Posted
Updated 30-Sep-16 2:30am
v6
Comments
F-ES Sitecore 29-Sep-16 9:24am    
Deserialise where? In .net code? Javascript? Have you googled "c# deserialise object" and followed the many articles and tutorials?
s yu 29-Sep-16 10:19am    
I did successfully for 2-level ( Level 1: object{object[]}, Level 2: object{string} object array from string (variable name: stream, see above). Now I need to do it on 3-level one. I need to deseriliaze the Level 1: object{object[]} into Level 2: object{object[]}. Then I should be able to further deseriliaze Level 2: object{object[]} to Level 3 object{string}. I got stuck on deseriliazing object{object[]} into another Level object{object[]}. Thanks for your review.
[no name] 29-Sep-16 16:05pm    
Another answer would be to read the answers you have already been given.

1 solution

You haven't stated an issue with your code, but since you expect someone to answer here is one option.

1) Use the Newtonsoft.Json library instead of the .net library - Json.NET - Newtonsoft[^]

2) Go to json2csharp.com and convert your json string to C# classes - json2csharp - generate c# classes from json[^]

This will create classes based on your nested/multi dimensional array that are serializeable/deserializable using JSON.net library

3) To serialize your class structure to JSON you would then do something like this

C#
string json = JsonConvert.SerializeObject(myJson2CsharpModel);


4) To deserialize it you would do something like

C#
MyJson2CsharpModel model = JsonConvert.DeserializeObject<MyJson2CsharpModel>(json);
 
Share this answer
 
Comments
s yu 30-Sep-16 6:54am    
David: Thank you VERY much for your response, esp. the source in 2). I tried the json2sharp for the json string (see below), which is generagted from
string json = JsonConvert.SerializeObject(stream, Formatting.Indented);
But I got the message below:
"Parsing your JSON didn't work. Please make sure it's valid. Already did that? Please let me know so I can fix it."
I should explain something additional here. In my JavaScript code, I create an Array, and then push 4 arrays (different structure) into this one. Then the composited array is POSTed through AJAX. In my .ashx, I got the json string.
In my case, I need to further deserialize an object, which contains 4 objects, into sub-objects if the sub-object is {object[]} rather than {object:string}. Do you hav any idea how to deal w/ this case? Thanks again.

// json string
"[[\"C04R032\",\"OB-C-3J\",\"C04\",\"TRACKWAY\",\"PRIVATE\",\"BRASS DISK\",38.900749999999995,-77.05172999999999],[\"75+82.00\",\"OUTBOUND\",\"\",\"\",\"SCS\"],[[\"6512\",\"IN INVERT\"],[\"15977\",\"null\"]],[[\"\",\"\",\"METRO\",\"5/28/1905\",\"SET\",\"388713.741\",\"785235.645\",\"\",\"\",\"\"],[\"\",\"ADJUSTED\",\"NAD27MD\",\"5/28/1905\",\"SET\",\"388691.5843\",\"785190.8866\",\"\",\"\",\"\"],[\"\",\"PGIS\",\"NAD83MD1991\",\"\",\"RECNORECORD\",\"449439.865\",\"1297611.009\",\"\",\"\",\"\"],[\"\",\"PGIS\",\"LDPNSRS2007\",\"\",\"RECNORECORD\",\"174553.7598\",\"194763.7164\",\"\",\"\",\"\"]]]"
s yu 3-Oct-16 13:24pm    
I have decided to adopt another approach to process the data. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900