[英]Parse array JSON Schema with Jackson
我定义了一个 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": "^(.*)$"
}
}
}
}
}
}
}
它始终是一个模式,首先包含项目,然后再包含属性。 在属性中可以有更多的 arrays 或找到的对象,所以我想递归地这样做。 我想要实现的是直接表示模式的Map<String, Object>
。 我遇到困难的是递归调用,其中当前属性是 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()卡住了。 当我将 object 属性拆箱时,它会转到 else 分支,在那里它将mappingId直接放入 Map,这在拆箱后是合乎逻辑的.. 但我猜我做错了.. 有人能指点我吗为了达到我想要的结果,我应该改变哪个? 谢谢 !!
我自己想通了。 也许它会帮助某人。
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);
}
private static Map<String, Object> fillHashMap(JsonNode rootNode) {
Map<String, Object> elementsMap = new HashMap<>();
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")) {
List<Map<String, Object>> objectArray = new ArrayList<>();
JsonNode itemsNode = field.getValue().get("items").get("properties");
objectArray.add(fillHashMap(itemsNode));
elementsMap.put(field.getKey(), objectArray);
} else if (field.getValue().get("type").toString().contains("object")) {
elementsMap.put(field.getKey(),
fillHashMap(field.getValue().get("properties")));
} else {
elementsMap.put(field.getKey(), field.getValue().get("default"));
}
}
return elementsMap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.