首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json加入JsonNode属性名

Json加入JsonNode属性名
EN

Stack Overflow用户
提问于 2014-09-29 20:19:11
回答 1查看 7.2K关注 0票数 5

我有这样一个模式:

代码语言:javascript
复制
{
  "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,我希望收集在其中‘请求’的节点属性名称。我该怎么做?我看不出有什么办法..。

代码语言:javascript
复制
             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.

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-30 19:31:31

您可以通过使用List findParents(String fieldName)来获取具有该属性的所有节点,这将为您提供该属性。从医生那里:

方法,用于在此节点或其后代中查找包含指定字段的JSON对象。如果在此节点或其后代中没有找到匹配字段,则返回null。

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

代码语言:javascript
复制
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));
}

输出:

代码语言:javascript
复制
{
  "type" : "integer",
  "required" : true
}
{
  "type" : "string",
  "required" : true
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26108156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档