For deserialising a json object, I had to define a parent class that would contain an object or an array of objects for the child class. It has to be an object if an object was retrieved, or an array of objects if an array was retrieved from the json.
JSON array object
{"y":{"x":[{"data":28}, {"data":56}, {"data":89}]}}
JSON object
{"y":{"x":{"data":28}}}
y is receiving x at a time, and x[] at another time. There is no such condition to determine whether y would recieve an array or an object.
Hence for determining whether I received an array or not, I am checking the IsArray() condition.
I tried
class Y
{
public X x { get { return System.IsArray() ? new X() : new x[] }; set; }
}
class X
{
public int data { get; set; }
}
- It isnt working.
- System.IsArray() isn’t being recognised??
First off, an array is an object. That’s a good thing, since it allows these functions to work (both assume
using System;):Second, if you want a property whose type can be either
XorX[], the property’s type needs to beobject:This somewhat ignores the advantage of static typing, as you’re using
objectand checking types at run time. It would really be much simpler to define the property as an array, even for those cases where there’s only one value. In such cases, it would be an array whose length is 1.