Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is Array or Object?

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; }
}
  1. It isnt working.
  2. System.IsArray() isn't being recognised??
like image 807
anurag Avatar asked Apr 12 '12 06:04

anurag


People also ask

How do you check if a variable is an object?

If typeof yourVariable === 'object' , it's an object or null .

How do you check if something is an array?

The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.


3 Answers

I've been using the Json.NET Nuget package, and it's been really easy to work with:

  string jsonStr = "{'y':{'x':[{'data':28}, {'data':56}, {'data':89}]}}";
  dynamic jobject = JsonConvert.DeserializeObject(jsonStr);

  bool isArray  = jobject.y.x.Type == JTokenType.Array;
  bool isObject = jobject.y.x.Type == JTokenType.Object;

Hope this helps!

like image 142
Branden Barber Avatar answered Nov 07 '22 22:11

Branden Barber


First off, an array is an object. That's a good thing, since it allows these functions to work (both assume using System;):

bool IsArray(object o) { return o is Array; }
bool IsArray(object o) { return o.GetType().IsArray; }

Second, if you want a property whose type can be either X or X[], the property's type needs to be object:

class Y             
{
   private object _x;
   public object x {
       get { return _x; }
       set
       {
           if (value.GetType != typeof(X) && value.GetType != typeof(X[]))
               throw new ArgumentException("value");
           _x = value;
       }
   }
}

This somewhat ignores the advantage of static typing, as you're using object and 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.

like image 40
phoog Avatar answered Nov 07 '22 23:11

phoog


the property x of Type X in class Y cannot be an array of X if you explicity state it is of type X. Declaring it as an object would be one way to get round this.

If you want to check it is an array I would use a backing field (say _x) and then where you use the property do a check (typeof(_x) == X[])

That could get messy though, my best advice would be set the type of property x to X[] and in the set determine whether the value was an array (if so just set _x = value) or if not add value to an empty array of X

like image 30
Daniel Elliott Avatar answered Nov 07 '22 22:11

Daniel Elliott