Java 8 - 使用JsonObject(javax.json) - 如何确定节点是否有子节点?

时间:2021-07-10 19:40:09

I've just started using javax.json package. I know how to extract values from JSON object in Java 8 if I know the structure of my JSON string. But how about strings I don't know the structure of? The question: how to determine if a node has child nodes?

我刚开始使用javax.json包。我知道如果我知道我的JSON字符串的结构,如何从Java 8中的JSON对象中提取值。但是字符串怎么样我不知道结构?问题:如何确定节点是否有子节点?

To read values, I simply need to use "get*" methods - it works OK, but there's no method like "checkIfArray" or "checkIfObject" to check if I even can use methods like "getString"...

要读取值,我只需要使用“get *”方法 - 它工作正常,但是没有像“checkIfArray”或“checkIfObject”这样的方法来检查我是否可以使用像“getString”这样的方法......

1 个解决方案

#1


6  

In javax.json package, both available types support emptiness checks because they both implement a java.collection interfaces:

在javax.json包中,两种可用类型都支持空白检查,因为它们都实现了java.collection接口:

JsonObject is-a java.util.Map<String, JsonValue>. As a result, one can check if such object contains any values by simply calling isEmpty() method.

JsonObject是一个java.util.Map 。因此,可以通过简单地调用isEmpty()方法来检查此类对象是否包含任何值。 ,jsonvalue>

JsonArray is-a java.util.List<JsonValue>. As a result, one - again - can check if the array is empty by calling isEmpty() method on it.

JsonArray是一个java.util.List 。结果,再次 - 可以通过调用isEmpty()方法来检查数组是否为空。

For traversing a tree of JsonStructures while marking all empty nodes, you can use this helper method:

要在标记所有空节点时遍历JsonStructures树,可以使用此辅助方法:

boolean isValueEmpty(JsonValue v) {
  if (v == null) {
    return true; // or you may throw an exception, for example
  }
  switch(v.getValueType()) {
    case NULL:
      return true; // same as with Java null, we assume that it is Empty
    case ARRAY: 
      return ((JsonArray) v).isEmpty();
      // additionally, you can say that empty array is array with only empty elements
      // this means a check like this:
      // return ((JsonArray v).stream().allMatch(isValueEmpty); // recurse
    case OBJECT:
      return ((JsonObject v).isEmpty();
    case STRING:
      // optionally: if you want to treat '' as empty
      return ((JsonString v).getString().isEmpty();
    default:
      return false; // to my knowledge, no other Json types can be empty
  }
}

#1


6  

In javax.json package, both available types support emptiness checks because they both implement a java.collection interfaces:

在javax.json包中,两种可用类型都支持空白检查,因为它们都实现了java.collection接口:

JsonObject is-a java.util.Map<String, JsonValue>. As a result, one can check if such object contains any values by simply calling isEmpty() method.

JsonObject是一个java.util.Map 。因此,可以通过简单地调用isEmpty()方法来检查此类对象是否包含任何值。 ,jsonvalue>

JsonArray is-a java.util.List<JsonValue>. As a result, one - again - can check if the array is empty by calling isEmpty() method on it.

JsonArray是一个java.util.List 。结果,再次 - 可以通过调用isEmpty()方法来检查数组是否为空。

For traversing a tree of JsonStructures while marking all empty nodes, you can use this helper method:

要在标记所有空节点时遍历JsonStructures树,可以使用此辅助方法:

boolean isValueEmpty(JsonValue v) {
  if (v == null) {
    return true; // or you may throw an exception, for example
  }
  switch(v.getValueType()) {
    case NULL:
      return true; // same as with Java null, we assume that it is Empty
    case ARRAY: 
      return ((JsonArray) v).isEmpty();
      // additionally, you can say that empty array is array with only empty elements
      // this means a check like this:
      // return ((JsonArray v).stream().allMatch(isValueEmpty); // recurse
    case OBJECT:
      return ((JsonObject v).isEmpty();
    case STRING:
      // optionally: if you want to treat '' as empty
      return ((JsonString v).getString().isEmpty();
    default:
      return false; // to my knowledge, no other Json types can be empty
  }
}