How to check if object is JProperty or JArray

我的梦境 提交于 2020-02-21 11:04:31

问题


Given the two JTokens:

{ "Users": { "Name": "Carl" } }

and

{ "Users": [ { "Name": "Carl" }, {"Name": "Peter"} ] }

How can I tell if Users is a JProperty or JObject/JArray?

I need loop Users with

foreach (JObject User in myjobject["Users"]) { ... }

Solution It was as simple as myjobject["Users"].GetType(). However, that didn't work in the Watch debugger window, but it worked at runtime. Hrmpff.


回答1:


The Type property will tell you the type of the token you have.

switch(token.Type)
{
     case JTokenType.Array:
         break;
     case JTokenType.String:
         break;
}


来源:https://stackoverflow.com/questions/18172549/how-to-check-if-object-is-jproperty-or-jarray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!