[英]Jackson Json accesing JsonNode property name
I have a schema like this: 我有这样的架构:
{
"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
}
}
I have the following code that converts this shcema draft 3 to draft 4 by removing "required", I want to collect nodes property names that have 'requred' in them. 我有以下代码通过删除“required”将此shcema draft 3转换为draft 4,我想收集其中包含'requred'的节点属性名称。 How do I do that? 我怎么做? I don't see methods for this.. 我没有看到这方面的方法..
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.
Thanks! 谢谢!
You can get all of the nodes that have that property by using the List<JsonNode> findParents(String fieldName)
, which does that this for you. 您可以使用List<JsonNode> findParents(String fieldName)
获取具有该属性的所有节点,这样可以为您完成此操作。 From the docs: 来自文档:
Method for finding a JSON Object that contains specified field, within this node or its descendants. 在此节点或其后代中查找包含指定字段的JSON对象的方法。 If no matching field is found in this node or its descendants, returns null. 如果在此节点或其后代中找不到匹配的字段,则返回null。
I made a quick example but had to add a few characters to the JSON blob you posted as it is missing some commas and a bracket and can't be read by the ObjectMapper. 我做了一个简单的例子但是必须在你发布的JSON blob中添加几个字符,因为它缺少一些逗号和一个括号,并且不能被ObjectMapper读取。 It is as simple as this: 这很简单:
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));
}
Output: 输出:
{
"type" : "integer",
"required" : true
}
{
"type" : "string",
"required" : true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.