我有一个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);
现在,我想在mongoDB中保存这个JSON数据。我想通过解析然后保存它来保存它。但我的数据太多无法解析。我想将所有数据保存到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()
您可以使用save
的DBCollection
方法将JSON直接保存到MongoDB中。
这是一个列表示例:
DB db = mongo.getDB("your DB");
DBCollection collection = db.getCollection("your Collection");
final Object myRestData = JSON.parse(buffer.toString());
collection.save((DBObject) myRestData );