我有以下...
JArray clients = (JArray)clientsParsed["objects"];
foreach (JObject item in clients.Children())
{
// etc.. SQL params stuff...
command.Parameters["@MyParameter"].Value = JTokenToSql(item["thisParameter"]);
}
JTokenToSql
看起来像这样...
public static object JTokenToSql(JToken obj)
{
if (obj.Any())
return (object)obj;
else
return (object)DBNull.Value;
}
我也试过 ((JObject)obj).Count
.. 但似乎没有用.
I have tried ((JObject)obj).Count
also.. But doesn't seem to be working.
要检查 JObject
上是否存在属性,可以使用方括号语法查看结果是否为 null.如果该属性存在,将始终返回一个 JToken
(即使它在 JSON 中具有值 null
).
To check whether a property exists on a JObject
, you can use the square bracket syntax and see whether the result is null or not. If the property exists, a JToken
will be always be returned (even if it has the value null
in the JSON).
JToken token = jObject["param"];
if (token != null)
{
// the "param" property exists
}
如果您手头有一个 JToken
并且您想查看它是否为非空,那么这取决于它是什么类型的 JToken
以及您如何使用定义空".我通常使用这样的扩展方法:
If you have a JToken
in hand and you want to see if it is non-empty, well, that depends on what type of JToken
it is and how you define "empty". I usually use an extension method like this:
public static class JsonExtensions
{
public static bool IsNullOrEmpty(this JToken token)
{
return (token == null) ||
(token.Type == JTokenType.Array && !token.HasValues) ||
(token.Type == JTokenType.Object && !token.HasValues) ||
(token.Type == JTokenType.String && token.ToString() == String.Empty) ||
(token.Type == JTokenType.Null);
}
}
这篇关于检查 JObject 中的空或空 JToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!