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

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

      1. JSONObject 删除空值对

        时间:2023-09-29

          <tfoot id='vX2PH'></tfoot>
            • <bdo id='vX2PH'></bdo><ul id='vX2PH'></ul>

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

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

                <tbody id='vX2PH'></tbody>
              • <legend id='vX2PH'><style id='vX2PH'><dir id='vX2PH'><q id='vX2PH'></q></dir></style></legend>
                  本文介绍了JSONObject 删除空值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我的 Json 文件:

                  Here is my Json File:

                  {  
                     "models":{},
                     "path":[  
                        {  
                           "path":"/web-profiles",
                           "operations":[  
                              {  
                                 "type":"",
                                 "responseMessages":[]
                              }
                           ]
                        }
                     ],
                     "produces":[]
                  }
                  

                  如果键的值为空(包括 []、""、{}).如何从 Json 文件中删除这些对.

                  If the values of keys are empty (includes [], "", {}). How can I remove those pairs from Json file.

                  1. 我尝试使用 JSONObject 内置函数来删除不必要的对.但是,它不起作用.
                  2. 我尝试使用字符串方法逐行处理.它有太多案例,我无法在我的代码中涵盖所有这些案例.(例如,子键 'operations',当你想删除所有空值时,这个 key(operations) 值对也应该被删除.)有什么想法吗?

                  推荐答案

                  如果你使用javax.api API:

                  If you use javax.api API:

                  public static JsonArray removeNull(JsonArray array) {
                      JsonArrayBuilder builder = Json.createArrayBuilder();
                      int i = 0;
                      for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
                          JsonValue value = it.next();
                          switch (value.getValueType()) {
                          case ARRAY:
                              JsonArray a = removeNull(array.getJsonArray(i));
                              if (!a.isEmpty())
                                  builder.add(a);
                              break;
                          case OBJECT:
                              JsonObject object = removeNull(array.getJsonObject(i));
                              if (!object.isEmpty())
                                  builder.add(object);
                              break;
                          case STRING:
                              String s = array.getString(i);
                              if (s != null && !s.isEmpty())
                                  builder.add(s);
                              break;
                          case NUMBER:
                              builder.add(array.getJsonNumber(i));
                              break;
                          case TRUE:
                          case FALSE:
                              builder.add(array.getBoolean(i));
                              break;
                          case NULL:
                              break;
                          }
                      }
                      return builder.build();
                  }
                  
                  public static JsonObject removeNull(JsonObject obj) {
                      JsonObjectBuilder builder = Json.createObjectBuilder();
                      for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
                          Entry<String, JsonValue> e = it.next();
                          String key = e.getKey();
                          JsonValue value = e.getValue();
                          switch (value.getValueType()) {
                          case ARRAY:
                              JsonArray array = removeNull(obj.getJsonArray(key));
                              if (!array.isEmpty())
                                  builder.add(key, array);
                              break;
                          case OBJECT:
                              JsonObject object = removeNull(obj.getJsonObject(key));
                              if (!object.isEmpty())
                                  builder.add(key, object);
                              break;
                          case STRING:
                              String s = obj.getString(key);
                              if (s != null && !s.isEmpty())
                                  builder.add(key, s);
                              break;
                          case NUMBER:
                              builder.add(key, obj.getJsonNumber(key));
                              break;
                          case TRUE:
                          case FALSE:
                              builder.add(key, obj.getBoolean(key));
                              break;
                          case NULL:
                              break;
                          }
                      }
                      return builder.build();
                  }
                  
                  @Test
                  public void testRemoveNullJsonObject() {
                      String str = ""
                          + "{"
                          + "   "models":{},"
                          + "   "path":["
                          + "      {"
                          + "         "path":"/web-profiles","
                          + "         "operations":["
                          + "            {"
                          + "               "nickname":"CreateAWebExperienceProfile","
                          + "               "type":"","
                          + "               "responseMessages":[]"
                          + "            }"
                          + "         ]"
                          + "      }"
                          + "   ],"
                          + "   "produces":[]"
                          + "}";
                      JsonObject json = Json.createReader(new StringReader(str)).readObject();
                      System.out.println(json);
                      JsonObject removed = removeNull(json);
                      System.out.println(removed);
                      // -> {"path":[{"path":"/web-profiles","operations":[{"nickname":"CreateAWebExperienceProfile"}]}]}
                  }
                  

                  这篇关于JSONObject 删除空值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用于检查字符串是否包含*无*异常的数字的 Java 下一篇:Java:当秒和毫秒都为 0 时,DateTimeFormatter 无法解析

                  相关文章

                  最新文章

                  1. <legend id='mKfAW'><style id='mKfAW'><dir id='mKfAW'><q id='mKfAW'></q></dir></style></legend>

                    • <bdo id='mKfAW'></bdo><ul id='mKfAW'></ul>

                    1. <small id='mKfAW'></small><noframes id='mKfAW'>

                      <tfoot id='mKfAW'></tfoot>

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