如何检查变量是数组还是对象?

时间:2022-08-01 07:01:29

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对象,我必须定义一个父类,它将包含子类的对象或对象数组。如果检索对象,它必须是对象;如果从json检索数组,则必须是对象数组。

JSON array object

JSON数组对象

{"y":{"x":[{"data":28}, {"data":56}, {"data":89}]}}

JSON object

JSON对象

{"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.

y一次接收到x,另一次接收到x[]。没有这样的条件来确定y是接收数组还是接收对象。

Hence for determining whether I received an array or not, I am checking the IsArray() condition.

因此,为了确定是否接收了数组,我将检查IsArray()条件。

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. 它不是工作。
  3. System.IsArray() isn't being recognised??
  4. System.IsArray()不被认可?

3 个解决方案

#1


13  

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:

其次,如果您想要一个类型可以是X或X[]的属性,则属性的类型必须为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.

这在一定程度上忽略了静态类型的优点,因为您在运行时使用对象并检查类型。将属性定义为数组要简单得多,即使在只有一个值的情况下也是如此。在这种情况下,它是一个长度为1的数组。

#2


4  

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

我一直在使用Json。NET Nuget包,非常容易使用:

  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!

希望这可以帮助!

#3


1  

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.

类Y中x类型的属性x不能是x的数组如果你显式地声明它是x类型的,将它声明为对象是绕过这个问题的一种方法。

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[])

如果你想检查它是一个数组,我会使用一个背景字段(比如_x)然后在你使用属性的地方做一个检查(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

我最好的建议是将属性x的类型设置为x[],并在集合中确定该值是数组(如果只设置_x = value)还是为x的空数组添加值

#1


13  

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:

其次,如果您想要一个类型可以是X或X[]的属性,则属性的类型必须为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.

这在一定程度上忽略了静态类型的优点,因为您在运行时使用对象并检查类型。将属性定义为数组要简单得多,即使在只有一个值的情况下也是如此。在这种情况下,它是一个长度为1的数组。

#2


4  

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

我一直在使用Json。NET Nuget包,非常容易使用:

  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!

希望这可以帮助!

#3


1  

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.

类Y中x类型的属性x不能是x的数组如果你显式地声明它是x类型的,将它声明为对象是绕过这个问题的一种方法。

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[])

如果你想检查它是一个数组,我会使用一个背景字段(比如_x)然后在你使用属性的地方做一个检查(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

我最好的建议是将属性x的类型设置为x[],并在集合中确定该值是数组(如果只设置_x = value)还是为x的空数组添加值