簡體   English   中英

使用BlueMix嘗試將JSON數據插入Cloudant數據庫中,而無需在Java中對文件格式進行硬編碼

[英]Using BlueMix trying to insert JSON data into a Cloudant database without having to Hard Code the format of the file inside Java

我為Bluemix Cloudant下載了“ Favorites”應用程序。 很好,直到我意識到“字段”已在服務器上的java文件中進行了硬編碼。 例如,ResourseServlet對象具有此硬編碼的create函數。

create(Database db, String id, String name, String value, Part part,
        String fileName) throws IOException {

伴隨着硬編碼的MAP構建

Map<String, Object> data = new HashMap<String, Object>();
data.put("name", name);
data.put("_id", id);
data.put("value", value);
data.put("creation_date", new Date().toString());

導致數據庫保存。

db.save(data);

如果您希望將所有json文件中的所有字段都硬編碼為java代碼,則可以使用這種類型的示例。

但這並不能解決問題,我可以想象大多數我使用的所有應用程序都返回以JSON格式預格式化的文本文件。

因此,我只需要簡單地擁有一個可以像“ FileInputStream”對象一樣接收的函數,或者,如果需要,我可以使用BufferedReader並將其讀入String並嘗試保存該對象。

我真的很喜歡cloudant數據庫,因為它存儲和檢索JSON,但是現在我有1000個JSON文件,我想在其中存儲不同的字段類型和結構,並且不能真正對每種類型的所有這些值對進行硬編碼。

是否有一個很酷的庫或函數,如果我已經有一個文件,它會像把它放在數據庫中一樣

db.save(JSONurl);   

哪里的JSONurl只是網絡上發送回JSON的URL?

謝謝你的幫助。 一直到凌晨三點才嘗試找到該方法。 (花了我幾天的時間才發現字段是在服務器代碼中硬編碼的。。。。)

我意識到我可以編寫一個循環,然后嘗試自己解析JSON數據並重新編碼,但是似乎應該有一些更簡單且可用的東西。

您可以使用db.save(JsonObject)完成所需的工作。

請參見下面的示例代碼。 我通過了一個硬編碼的JSON字符串作為示例測試,但是您可以根據需要從文件或URL中讀取JSON字符串。 我還使用了Google的JsonObject和JsonParser,它們在Cloudant“收藏夾”示例中可用。

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

String JSONString = new String ("{ \"hot\": { \"season\": \"summer\", \"weather\": \"usually warm and sunny\" }, \"cold\": { \"season\": \"winter\", \"weather\": \"usually cold and snowy\" }, \"sneezy\": { \"season\": \"spring\", \"weather\": \"cool with rain and sun\" },\"colorful\": {\"season\": \"autumn\",\"weather\": \"breezes\"} }");              
JsonParser parser = new JsonParser();
JsonObject data = (JsonObject)parser.parse(JSONString);              
db.save(data);

這將在Cloudant中創建以下文檔:

{
  "_id": "9ec91813106c4aa69ad38e42b268e1f8",
  "_rev": "1-1d8ab47557aaeb1ac342cd4f5f153f16",
  "hot": {
    "season": "summer",
    "weather": "usually warm and sunny"
  },
  "cold": {
    "season": "winter",
    "weather": "usually cold and snowy"
  },
  "sneezy": {
    "season": "spring",
    "weather": "cool with rain and sun"
  },
  "colorful": {
    "season": "autumn",
    "weather": "breezes"
  }
}

感謝您對JsonParser對象的響應 當我進行更多研究時,我想出了如何使用Google的Gson Object做類似的事情,在Google中,我使用Json格式的字符串或返回JSON的URL構建HashMap。 我使用循環從輸入文件構建字符串,但是也許可以避免該步驟,但是它可以工作。

import com.google.gson.Gson;
import java.util.HashMap;
import java.io.BufferedReader;  
import java.io.InputStreamReader;   
import java.lang.StringBuilder;    
import java.net.URL;

...

URL myURL = new URL("http://... some url that returns JSON");
BufferedReader reader = new BufferedReader(new InputStreamReader(myURL.openStream()));

while (true) {
            iLine=reader.readLine();
            if (iLine == null) {
            break;
            }  // end if
            stringBuilder.append(iLine);
        } // end while
        reader.close();

HashMap<String, Object> myMap = gson.fromJson(stringBuilder.toString(), HashMap.class);

        myMap.put("_id", id + "");
        myMap.put("value", "Writing From URL Object String V2");
        db.save(myMap);

...   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM