我正在尝试type
使用最节省内存和高性能的方法来查找 JSON Schema 中的任何元素。但是,在达到某个点后,我有点卡住并且没有想法,所以希望在这里得到答案:
基本上,我有一个List: jsonPath
由元素及其父元素组成,我需要从Json Schema
我拥有的元素中识别其类型。为了解析Json Schema
我正在使用Java Jackson Library
. 我能够获得所有元素并能够找到它的类型,但在比较过程中,我有点困惑。
以下是我到目前为止的代码:正如我们所见,我有jsonPath
as"food", "Ingredients", "ingredient"
这意味着我需要找到type
最后一个元素的ingredient
,而其他元素是它的父母。该程序应返回type
为array
. 同样,如果我传递任何其他元素,jsonPath array
例如 justfood
或price
then 它应该检查相应的父母和他们的孩子,那么最终应该返回它的类型。我Stack elements
只是出于参考目的,您可以忽略它。
我可以尝试if
在每个阶段添加多个条件并获取元素类型。但是,我试图找到一种简单且更好的方法,而不是使用复杂的嵌套 if。应该可以以一种非常简单的方式进行操作,但目前对我来说没有任何点击。
public class JsonElementLocator {
private final JsonNode outerNode;
private static JsonElementLocator _instance;
// Default constructor to get the content and store in the root
private JsonElementLocator() throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode root = mapper.readTree(JsonElementLocator.class.getClassLoader().getResource("testJSON.json"));
outerNode = root.path("definitions");
}
// Method to create a Object instance of current class
private static synchronized JsonElementLocator getInstance() throws IOException {
if (_instance == null) {
_instance = new JsonElementLocator();
}
return _instance;
}
// Method called by other application to get the element Type
public static Optional < String > locate(List < String > elementPath) throws IOException {
JsonElementLocator cl = getInstance();
JsonNode rootNode = cl.outerNode;
recurse(rootNode, new Stack < String > ());
return Optional.empty();
}
// Method called recursively to get the element Type from JSON Schema
private static Optional < String > recurse(JsonNode rootNode, Stack < String > elements) {
// Loop through each Object in Root Node
for (JsonNode childNode: rootNode) {
// Check if childNode is of Object type
if (childNode.path("type").toString().contains("object")) {
elements.push(childNode.get("title").toString());
// Call recurse method for the subsequent objects
recurse(childNode.path("properties"), elements);
elements.pop();
} else if (childNode.path("type").toString().contains("array")) {
// Check if childNode is of Array type and call its elements
for (JsonNode ccNode: childNode.path("items")) {
final String def = ccNode.toString().contains("definitions") ? ccNode.toString().replace("\"", "").replaceAll("#/definitions/",
"") : null;
// Find the element from the root
if (def != null) {
final JsonNode defInfo = _instance.outerNode.get(def);
// Call recurse method for definition elements
recurse(defInfo.path("properties"), elements);
}
}
} else {
System.out.println(String.join("/", elements));
// Check if childNode is of normal type
}
}
return Optional.empty();
}
public static void main(String[] args) throws IOException {
List < String > jsonPath = new ArrayList < String > (List.of("food", "Ingredients", "ingredient"));
locate(jsonPath);
}
}
以下是Json Schema
我要解析的完整内容:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome2",
"definitions": {
"Welcome2": {
"type": "object",
"additionalProperties": false,
"properties": {
"hotelName": {
"type": "string"
},
"food": {
"type": "array",
"items": {
"$ref": "#/definitions/Food"
}
}
},
"required": [
"food",
"hotelName"
],
"title": "Welcome2"
},
"Food": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"price": {
"type": "string"
},
"description": {
"type": "string"
},
"calories": {
"type": "string",
"format": "integer"
},
"ingredients": {
"$ref": "#/definitions/Ingredients"
}
},
"required": [
"calories",
"description",
"ingredients",
"name",
"price"
],
"title": "Food"
},
"Ingredients": {
"type": "object",
"additionalProperties": false,
"properties": {
"ingredient": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"ingredient"
],
"title": "Ingredients"
}
}
}