使用Java将复杂的嵌套JSON插入MongoDB

brc7rcf0  于 2023-02-17  发布在  Java
关注(0)|答案(1)|浏览(191)

我有一个复杂的嵌套JSON文件,如下所示

{
  "databases": {
    "component": "pages/dems/transparency/workspaces/DatasourcesWorkspace.vue",
    "children": [
      {
        "name": "Databases",
        "path": "databases",
        "component": "pages/dems/transparency/workspaces/Databases.vue",
        "hide": true,
        "help": "context-sensitive-help/transparency/workspaces/datasources/databases/databases/databases.html"
      },
      {
        "name": "Schemas",
        "path": "schemas",
        "component": "containers/EmptyContainer.vue",
        "hide": true,
        "children": [
          {
            "name": "",
            "component": "pages/dems/transparency/workspaces/Schemas.vue",
            "hide": true,
            "help": "context-sensitive-help/transparency/workspaces/datasources/databases/schemas.html"
          },
          {
            "name": "",
            "path": "relationship/:id",
            "component": "pages/dems/transparency/workspaces/profilecolumn/ViewRelationShip.vue",
            "hide": true,
            "help": "context-sensitive-help/transparency/workspaces/relationship/relationship.html"
          }
        ]
      }
    ]
  }
}

我想把它插入MongoDB,结构如下:

{
    "_id":"id1",
    "field":"databases",
    "value":{
    "component": "pages/dems/transparency/workspaces/DatasourcesWorkspace.vue",
    "children": [
      {
        "name": "Databases",
        "path": "databases",
        "component": "pages/dems/transparency/workspaces/Databases.vue",
        "hide": true,
        "help": "context-sensitive-help/transparency/workspaces/datasources/databases/databases/databases.html"
      },
      {
        "children": [
          {
            "name": "",
            "component": "pages/dems/transparency/workspaces/Schemas.vue",
            "hide": true,
            "help": "context-sensitive-help/transparency/workspaces/datasources/databases/schemas.html"
          },
          {
            "name": "",
            "path": "relationship/:id",
            "component": "pages/dems/transparency/workspaces/profilecolumn/ViewRelationShip.vue",
            "hide": true,
            "help": "context-sensitive-help/transparency/workspaces/relationship/relationship.html"
          }
        ]
      }
    ]
  }
}

我还需要检查是否有任何对象在component值中包含containers/EmptyContainer.vue,然后不要将该对象插入MongoDB中。我尝试了许多方法,但都不起作用。
已尝试代码,但未按预期工作:

public ArrayList<Document> processJsonV4(Object source) throws JSONException {
    ArrayList<Document> rootDoc = new ArrayList<>();
    if (source instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) source;
        Document document = new Document();
        if (jsonObject.has("component") && !"containers/EmptyContainer.vue".equals(jsonObject.getString("component"))) {
            for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) {
                String key = (String) keys.next();
                Object value = jsonObject.get(key);
                document.append(key, value);
            }

        } else {
            for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) {
                String key = (String) keys.next();
                Object value = jsonObject.get(key);
                processJsonV4(value);
            }
        }
        rootDoc.add(document);
    }
    KLogger.info("RootDoc: " + rootDoc);
    return rootDoc;
}

我如何使用Java实现这一点?

wvyml7n5

wvyml7n51#

一种简单的递归方法是在调用文档时构造并添加到文档列表中:

public void readAndConstructMongoDoc() {
  JSONObject jsonData = readFileData("<your-file.json>");
  List<Document> rootDoc = new ArrayList<>();
  constructRootDoc(rootDoc, jsonData);
  
  // Save in mongo -> rootDoc
}

private void constructRootDoc(List<Document> rootDoc, Object source) {
  if (source instanceof JSONObject) {
    JSONObject jsonObject = new JSONObject();
    if (jsonObject.has("component") && !"containers/EmptyContainer.vue".equals(jsonObject.getString("component"))) {
      rootDoc.add(convertToMongoDocument(jsonObject));

      JSONArray children = jsonObject.getJSONArray("children");
      int length = children.length();
      for (int i = 0; i < length; i++) {
        // Recursively call for all child node and pass the rootDoc
        testJson(rootDoc, children.getJSONObject(i));
      }
    }
  }
}

private Document convertToMongoDocument(JSONObject jsonObject) {
  Document document = new Document();
  document.append("name", jsonObject.getString("name"));
  document.append("path", jsonObject.getString("path"));
  document.append("hide", Boolean.valueOf(jsonObject.getString("hide")));
  document.append("component", jsonObject.getString("component"));
  return document;
}

边注:我相信你的Mongo文档模式可以即兴发挥,并不一定要以这种格式存储数据。

相关问题