Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jackson Json accesing JsonNode属性名称_Java_Json_Jackson - Fatal编程技术网

Java Jackson Json accesing JsonNode属性名称

Java Jackson Json accesing JsonNode属性名称,java,json,jackson,Java,Json,Jackson,我有这样一个模式: { "type" : "object", "$schema" : "http://json-schema.org/draft-03/schema#", "id" : "urn:jsonschema:com:vlashel:dto:UserDto", "description" : "this is the top description", "title" : "this is the top title", "properties" : {

我有这样一个模式:

{
  "type" : "object",
  "$schema" : "http://json-schema.org/draft-03/schema#",
  "id" : "urn:jsonschema:com:vlashel:dto:UserDto",
  "description" : "this is the top description",
  "title" : "this is the top title",
  "properties" : {
    "number" : {
      "type" : "integer"
      "required" : true
    },
    "password" : {
      "type" : "string"
      "required" : true

    }
}
我有以下代码,通过删除“required”将shcema草案3转换为草案4,我想收集其中包含“requre”的节点属性名称。我该怎么做?我看不出这方面的方法

             JsonNode jsonNode = jsonNodeIterator.next();
            ObjectNode element;
            if (jsonNode instanceof ObjectNode) {
                element = (ObjectNode) jsonNode;
                element.remove("required");
               String propertyName = element.getPropertyName(); //I'm looking for this kind of method.

谢谢

您可以通过使用获取具有该属性的所有节点,它可以为您这样做。从文档中:

方法中查找包含指定字段的JSON对象 此节点或其子节点。如果在此字段中找不到匹配字段 节点或其子节点返回null

我做了一个简单的示例,但不得不在您发布的JSON blob中添加几个字符,因为它缺少一些逗号和括号,ObjectMapper无法读取。就这么简单:

JsonNode root = mapper.readTree(SCHEMA);
List<JsonNode> required = root.findParents("required");
for (JsonNode node: required) {
    Object prettyOutput = mapper.readValue(node, Object.class);
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(prettyOutput));
}

哪家酒店?
ObjectNode
是具有多个属性的对象。您可以使用
迭代器getFields()
对它们进行迭代,但我不确定您在寻找什么。这实际上就是我想要的)谢谢
{
  "type" : "integer",
  "required" : true
}
{
  "type" : "string",
  "required" : true
}