• <i id='WanBB'><tr id='WanBB'><dt id='WanBB'><q id='WanBB'><span id='WanBB'><b id='WanBB'><form id='WanBB'><ins id='WanBB'></ins><ul id='WanBB'></ul><sub id='WanBB'></sub></form><legend id='WanBB'></legend><bdo id='WanBB'><pre id='WanBB'><center id='WanBB'></center></pre></bdo></b><th id='WanBB'></th></span></q></dt></tr></i><div id='WanBB'><tfoot id='WanBB'></tfoot><dl id='WanBB'><fieldset id='WanBB'></fieldset></dl></div>
  • <legend id='WanBB'><style id='WanBB'><dir id='WanBB'><q id='WanBB'></q></dir></style></legend>

        <bdo id='WanBB'></bdo><ul id='WanBB'></ul>
      <tfoot id='WanBB'></tfoot>

      <small id='WanBB'></small><noframes id='WanBB'>

        检查 JObject 中的空或空 JToken

        时间:2023-08-22
        <i id='LANCb'><tr id='LANCb'><dt id='LANCb'><q id='LANCb'><span id='LANCb'><b id='LANCb'><form id='LANCb'><ins id='LANCb'></ins><ul id='LANCb'></ul><sub id='LANCb'></sub></form><legend id='LANCb'></legend><bdo id='LANCb'><pre id='LANCb'><center id='LANCb'></center></pre></bdo></b><th id='LANCb'></th></span></q></dt></tr></i><div id='LANCb'><tfoot id='LANCb'></tfoot><dl id='LANCb'><fieldset id='LANCb'></fieldset></dl></div>
          <tbody id='LANCb'></tbody>

        <small id='LANCb'></small><noframes id='LANCb'>

        <tfoot id='LANCb'></tfoot>

          1. <legend id='LANCb'><style id='LANCb'><dir id='LANCb'><q id='LANCb'></q></dir></style></legend>
                  <bdo id='LANCb'></bdo><ul id='LANCb'></ul>

                  本文介绍了检查 JObject 中的空或空 JToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下...

                  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模板网!

                  上一篇:JObject.Parse 与 JsonConvert.DeserializeObject 下一篇:使用 Json.NET 将任何类型的对象转换为 JObject

                  相关文章

                  最新文章

                  <i id='Vqybr'><tr id='Vqybr'><dt id='Vqybr'><q id='Vqybr'><span id='Vqybr'><b id='Vqybr'><form id='Vqybr'><ins id='Vqybr'></ins><ul id='Vqybr'></ul><sub id='Vqybr'></sub></form><legend id='Vqybr'></legend><bdo id='Vqybr'><pre id='Vqybr'><center id='Vqybr'></center></pre></bdo></b><th id='Vqybr'></th></span></q></dt></tr></i><div id='Vqybr'><tfoot id='Vqybr'></tfoot><dl id='Vqybr'><fieldset id='Vqybr'></fieldset></dl></div>
                    <bdo id='Vqybr'></bdo><ul id='Vqybr'></ul>
                  <tfoot id='Vqybr'></tfoot>

                    <small id='Vqybr'></small><noframes id='Vqybr'>

                    <legend id='Vqybr'><style id='Vqybr'><dir id='Vqybr'><q id='Vqybr'></q></dir></style></legend>