java - 解析 JSON 数据并将其保存到 MongoDB

标签 java json mongodb

我有一个 JSON 数据,它保存在一个文件中,如下所示。

{"@odata.context":"/redfish/v1/$metadata#ServiceRoot(Oem,Id,Name,RedfishVersion,UUID,Links,Systems,Chassis,Managers,Tasks,SessionService,AccountService,EventService,Registries,JsonSchemas)","@odata.id":"/redfish/v1/","@odata.type":"#ServiceRoot.v1_0_5.ServiceRoot","Oem":{"ts_fujitsu":{"@odata.type":"http://ts.fujitsu.com/redfish-schemas/v1/FTSSchema.v1_0_0#FTSServiceRoot.v1_0_0.FTSServiceRoot","FileDownload":{"@odata.id":"/redfish/v1/Oem/ts_fujitsu/FileDownload"},"CASConfiguration":{"Enabled":false,"DisplayLoginPage":false,"ServerLoginUrl":null,"ServerLogoutUrl":null}}},"Id":"RootService","Name":"Root Service","RedfishVersion":"1.0.5","UUID":"74fce745-28ad-410a-bbf8-9291d486b457","Links":{"Oem":{},"Sessions":{"@odata.id":"/redfish/v1/SessionService/Sessions"}},"Systems":{"@odata.id":"/redfish/v1/Systems"},"Chassis":{"@odata.id":"/redfish/v1/Chassis"},"Managers":{"@odata.id":"/redfish/v1/Managers"},"Tasks":{"@odata.id":"/redfish/v1/TaskService"},"SessionService":{"@odata.id":"/redfish/v1/SessionService"},"AccountService":{"@odata.id":"/redfish/v1/AccountService"},"EventService":{"@odata.id":"/redfish/v1/EventService"},"Registries":{"@odata.id":"/redfish/v1/Registries"},"JsonSchemas":{"@odata.id":"/redfish/v1/JsonSchemas"},"@Redfish.Copyright":"Copyright 2014-2017 Distributed Management Task Force, Inc. (DMTF). For the full DMTF copyright policy, see http://www.dmtf.org/about/policies/copyright.","@odata.etag":"1511366122"}

现在我使用 Java 将此数据保存在 JSONBject 中。我需要解析这些数据并将其保存到 MongoDB。 我从 URL 获取这些数据。检索数据并将其保存到文件的代码如下。

public JsonObject authen() {
    JsonObject myRestData = new JsonObject();
    try{
          URL myUrl = new URL("http://{physical-system}/redfish/v1");
          URLConnection urlCon = myUrl.openConnection();
          urlCon.setRequestProperty("Method", "GET");
          urlCon.setRequestProperty("Accept", "application/json");
          urlCon.setConnectTimeout(5000);
          //set the basic auth of the hashed value of the user to connect
          urlCon.addRequestProperty("Authorization", GetMyCredentials() );
          InputStream is = urlCon.getInputStream();
          InputStreamReader isR = new InputStreamReader(is);
          BufferedReader reader = new BufferedReader(isR);
          StringBuffer buffer = new StringBuffer();
          String line = "";
          while( (line = reader.readLine()) != null ){
            buffer.append(line);
          }
          reader.close();
          JsonParser parser = new JsonParser();
          myRestData = (JsonObject) parser.parse(buffer.toString());

          return myRestData;

        }catch( MalformedURLException e ){
          e.printStackTrace();
          myRestData.addProperty("error", e.toString());
          return myRestData;
        }catch( IOException e ){
          e.printStackTrace();
          myRestData.addProperty("error", e.toString());
          return myRestData;
        }
}

public void writer(JsonObject o) throws JSONException, IOException {
    try (FileWriter file = new FileWriter("file1.json")) {
        file.write(o.toString());
        System.out.println("Successfully Copied JSON Object to File...");
        System.out.println("\nJSON Object: " + o);
    }
public void parser(JsonObject o) throws JSONException {
    JsonElement n = o.get("UUID");
    System.out.println(n);

现在,我想将此 JSON 数据保存在 mongoDB 中。我想通过解析它来保存它,然后保存它。但我的数据太多了,无法解析。我想将所有数据保存到 MongoDB 中。如果我想要数据库中的任何内容,我可以轻松查询它。我正在考虑一个聪明的方法来做到这一点。 [我是 MongoDB 和 JSON 的新手]

最佳答案

您应该使用最新版本的mongo-java-driver你可以简单地写

MongoClient client = new MongoClient("<your connection string (optional)>");
MongoCollection<Document> collection = client.getDatabase("<Database>").getCollection("<Collection>");
Document doc = Document.parse(<Your JSON string>);
collection.insertOne(doc);

您的 JSON 字符串将是 buffer.toString()

关于java - 解析 JSON 数据并将其保存到 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47938389/

相关文章:

C++ nonlohmann json读取子对象

javascript - 如何从多行和多列的表中获取选中的复选框并作为帖子响应发送

mongodb - docker-compose无法为mongodb创建用户和密码

json - 在 Swift 中循环遍历 JSON 对象

java - 不再支持将 UserCredentials 与 MongoClient 一起使用

node.js - 反转 : how to bind Model<Document> to container object

java - 系统中的两个不同的 UDP 套接字可以绑定(bind)同一个端口吗?

java - 如何从不同类的静态方法获取调用者类名

给定长度的Java随机数

java - 在 Netbeans 中监视 HTTP 请求和 WebSocket