9

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.

4

1 回答 1

18

Type属性将告诉您您拥有的令牌的类型。

switch(token.Type)
{
     case JTokenType.Array:
         break;
     case JTokenType.String:
         break;
}
于 2013-08-12T05:49:17.487 回答