我定义了一个JSON模式:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "array",
"title": "The Root Schema",
"items": {
"$id": "#/items",
"type": "object",
"title": "The Items Schema",
"required": [
"test",
"isExpand",
"numberOfIssue",
"issue",
"listOfDetails"
],
"properties": {
"test": {
"$id": "#/items/properties/test",
"type": "string",
"title": "The Test Schema",
"default": "",
"examples": [
"value"
],
"pattern": "^(.*)$"
},
"isExpand": {
"$id": "#/items/properties/isExpand",
"type": "boolean",
"title": "The Isexpand Schema",
"default": false,
"examples": [
true
]
},
"numberOfIssue": {
"$id": "#/items/properties/numberOfIssue",
"type": "integer",
"title": "The Numberofissue Schema",
"default": 0,
"examples": [
1
]
},
"issue": {
"$id": "#/items/properties/issue",
"type": "object",
"title": "The Issue Schema",
"required": [
"mappingId"
],
"properties": {
"mappingId": {
"$id": "#/items/properties/issue/properties/mappingId",
"type": "string",
"title": "The Mappingid Schema",
"default": "",
"examples": [
"1561561"
],
"pattern": "^(.*)$"
}
}
},
"listOfDetails": {
"$id": "#/items/properties/listOfDetails",
"type": "array",
"title": "The listOfDetails Schema",
"items": {
"$id": "#/items/properties/listOfDetails/items",
"type": "object",
"title": "The Items Schema",
"required": [
"self",
"detailId"
],
"properties": {
"self": {
"$id": "#/items/properties/listOfDetails/items/properties/self",
"type": "string",
"title": "The Self Schema",
"default": "",
"examples": [
"self1"
],
"pattern": "^(.*)$"
},
"issueId": {
"$id": "#/items/properties/listOfDetails/items/properties/detailId",
"type": "string",
"title": "The detailId Schema",
"default": "",
"examples": [
"000188181"
],
"pattern": "^(.*)$"
}
}
}
}
}
}
}
它将始终是一个架构,该架构首先包含项目,然后它将包含属性。
在属性中可以找到更多的数组或对象,所以我想递归地做到这一点。
我正在尝试实现的是一个Map<String, Object>
,它直接表示架构。我陷入困境的地方是递归调用,其中当前属性是对象或数组。
我想实现这一目标:
{
"test" : "",
"isExpand" : false,
"numberOfIssues" : 0,
"issue" : {
"mappingId" : ""
},
"listOfDetails" : [
{
"self" : "",
"detailId" : ""
}
]
}
这是我从文件中解析JsonSchema并从中获取实际属性的方法
private static void parseJsonNode(String path) throws Exception {
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
JsonNode rootNode = mapper.readTree(new File(METADATA_SCHEMA_PATH + path));
Map<String, Object> elementsMap = new HashMap<>();
fillHashMap(elementsMap, rootNode.get("items").get("properties"));
}
elementsMap
是全局定义的Map<String, Object>
Map<String, Object> elementsMap = new HashMap<>();
private static Map<String, Object> fillHashMap(Map<String, Object> elementsMap, JsonNode rootNode) throws Exception {
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
while (fieldsIterator.hasNext()) {
Map.Entry<String, JsonNode> field = fieldsIterator.next();
if (field.getValue().get("type").toString().contains("array")) {
//TODO HOW TO HANDLE ARRAY THERE
} else if (field.getValue().get("type").toString().contains("object")) {
elementsMap.put(field.getKey(), fillHashMap(elementsMap, field.getValue().get("properties")));
} else {
elementsMap.put(field.getKey(), field.getValue().get("default"));
}
}
return elementsMap;
}
我陷入了递归调用fillHashMap()的困境。当我将对象属性取消装箱时,它会转到else分支,将 mappingId 直接放到地图上,这在取消装箱后是合乎逻辑的..但是我猜测我做错了所有事情。有人可以指出我应该改变的东西,以便达到预期的效果吗?谢谢!!
答案 0 :(得分:0)
我自己弄清楚了。也许它会一次帮助别人。
12345
private static void parseJsonNode(String path) throws Exception {
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
JsonNode rootNode = mapper.readTree(new File(BASE_PATH + path));
Map<String, Object> elementsMap = fillHashMap(rootNode.get("items").get("properties"));
System.out.println(elementsMap);
}