Java 解析JSON数据并将其保存到MongoDB
我有一个JSON数据,保存在一个文件中,如下所示Java 解析JSON数据并将其保存到MongoDB,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.
{"@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将这些数据保存在JSONObject中。我需要解析这些数据并将其保存到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的新手]您可以使用
DBCollection
的save
方法将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 );
您应该使用最新版本的,您可以简单地编写
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);
MongoClient客户端=新的MongoClient(“”);
MongoCollection collection=client.getDatabase(“”).getCollection(“”);
Document doc=Document.parse();
托收。插入通(doc);
您的JSON字符串将是buffer.toString()JSON字符串的意思是:JSON文件路径?我仍然没有得到buffer.toString()的概念。我把资料存档了。你能带我看一下吗?你正在把整个网站内容(JSON)读入StringBuffer。因此,您可以将JSON字符串解析为文档或JsonObject或其他内容。如果文件中有数据,只需将文件完全读入
StringBuffer
,并将其解析为例如文档
。(您应该使用StringBuilder
而不是StringBuffer
tho)但没有成功完成。我收到一个错误“JSON阅读器需要一个值,但找到了“java”。您能告诉我如何避免此错误:java.lang.IllegalArgumentException:Invalid BSON field name@odata.context在我的JSON文件中,第一个字符是odata.context。我什么都做不了,看。BSON(MongoDB JSON格式)不允许JSON键中使用“.”或“$”。您只需删除所有“@odata”。这些只是一些不必要的前缀。