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
}