问题
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