Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用Jackson解析数组JSON模式_Java_Json_Jackson_Schema - Fatal编程技术网

Java 用Jackson解析数组JSON模式

Java 用Jackson解析数组JSON模式,java,json,jackson,schema,Java,Json,Jackson,Schema,我定义了一个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

我定义了一个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
。我遇到的问题是递归调用,其中当前属性是对象或数组

我想做到这一点:

{
  "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"));
    }
private static Map fillHashMap(Map elementsMap,JsonNode rootNode)引发异常{
迭代器fieldsIterator=rootNode.fields();
while(fieldsIterator.hasNext()){
Map.Entry field=fieldsIterator.next();
if(field.getValue().get(“type”).toString()包含(“数组”)){
//TODO如何处理那里的数组
}else if(field.getValue().get(“type”).toString()包含(“对象”)){
elementsMap.put(field.getKey(),fillHashMap(elementsMap,field.getValue().get(“属性”));
}否则{
elementsMap.put(field.getKey(),field.getValue().get(“默认”));
}
}
返回元素映射;
}

我被递归调用fillHashMap()卡住了。当我取消对对象属性的装箱时,它会转到else分支,在那里它会将mappingId直接放在地图上,这在取消装箱后是合乎逻辑的。。但我猜我做错了。。有人能告诉我为了达到我想要的结果我应该改变什么吗?谢谢

我自己想出来的。也许它会帮助某人

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 void parseJsonNode(字符串路径)引发异常{
ObjectMapper mapper=新的ObjectMapper(新的JsonFactory());
JsonNode rootNode=mapper.readTree(新文件(BASE_PATH+PATH));
Map elementsMap=fillHashMap(rootNode.get(“items”).get(“properties”);
系统输出打印LN(elementsMap);
}
private静态映射fillHashMap(JsonNode rootNode)引发异常{
Map elementsMap=newhashmap();
迭代器fieldsIterator=rootNode.fields();
while(fieldsIterator.hasNext()){
Map.Entry field=fieldsIterator.next();
if(field.getValue().get(“type”).toString()包含(“数组”)){
List objectArray=new ArrayList();
JsonNode itemsNode=field.getValue().get(“items”).get(“properties”);
add(fillHashMap(itemsNode));
elementsMap.put(field.getKey(),objectArray);
}else if(field.getValue().get(“type”).toString()包含(“对象”)){
elementsMap.put(field.getKey(),fillHashMap(field.getValue().get(“属性”));
}否则{
elementsMap.put(field.getKey(),field.getValue().get(“默认”));
}
}
返回元素映射;
}

看看我的@tsarenkotxt。。实际上,在foreach的示例中,它并没有按预期的方式工作。。
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;
}
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) throws Exception {
    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;
}