How to check if a variable is Array or Object?

前端 未结 3 486
后悔当初
后悔当初 2021-01-11 14:24

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

3条回答
  •  臣服心动
    2021-01-11 15:02

    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!

提交回复
热议问题