2015-12-15 88 views
1

我是NoSQL數據庫的新手。我想將以下JSON數據保存到CouchBase Lite中。有人可以指導我以最好的方式來做到這一點嗎?如何將數據插入到JSON格式的couchbase lite android

{"widget": { 

    "window": { 
     "title": "Sample Konfabulator Widget", 
     "name": "main_window", 
     "width": 500, 
     "height": 500 
    }, 
    "image": { 
     "src": "Images/Sun.png", 
     "name": "sun1", 
     "hOffset": 250, 
     "vOffset": 250, 
     "alignment": "center" 
    }, 
    "text": { 
     "data": "Click Here", 
     "size": 36, 
     "style": "bold", 
     "name": "text1", 
     "hOffset": 250, 
     "vOffset": 100, 
     "alignment": "center", 
     "onMouseUp": "sun1.opacity = (sun1.opacity/100) * 90;" 
    } 
}}  

我試着用下面的代碼來做到這一點。

public void insertSample(String widget,String control,ArrayList<eModel> details){ 

    Map<String, Object> properties = new HashMap<String, Object>(); 
    properties.put("control", control); 
    properties.put("details", details); 
    Document document = database.getDocument(widget); 
    try { 
     document.putProperties(properties); 
    } catch (CouchbaseLiteException e) { 
     Log.e("", "Cannot save document", e); 
    } 
} 

但是這段代碼每次都會創建一個新的id。我想多次插入相同的窗口小部件值。

這是運行時數據,而不是我想要逐個插入的靜態數據。

例如,發出的Widget圖如下:

{"widget": { 

     "window": { 
      "title": "Sample Konfabulator Widget", 
      "name": "main_window", 
      "width": 500, 
      "height": 500 
     }, 
} 

然後我要追加下的「窗口」字段以下字段:

"image": { 
      "src": "Images/Sun.png", 
      "name": "sun1", 
      "hOffset": 250, 
      "vOffset": 250, 
      "alignment": "center" 
     }, 
} 
+0

,如果你想使用相同的部件值多次,您可以更新 –

+0

@pushpendrachauhan如果我更新,然後怎麼樣舊數據? –

回答

0

你需要建立你的地圖狀這樣可以保存上面的json格式。

Map<String, Object> windowMap = new HashMap<String, Object>(); 
    windowMap.put("title","Sample Konfabulator Widget"); 
    windowMap.put("name","main_window"); 
    windowMap.put("width",500); 
    windowMap.put("height",500); 

    Map<String, Object> imageMap = new HashMap<String, Object>(); 
    imageMap.put("src","Images/Sun.png"); 
    imageMap.put("name","sun1"); 
    imageMap.put("hOffset",250); 
    imageMap.put("vOffset",250); 

    Map<String, Object> textMap = new HashMap<String, Object>(); 
    textMap.put("data","Click Here"); 
    textMap.put("size",36); 


    Map<String, Object> memberMap = new HashMap<String, Object>(); 
    memberMap.put("window",windowMap); 
    memberMap.put("image",imageMap); 
    memberMap.put("text",textMap); 

    Map<String, Object> widgetMap = new HashMap<String, Object>(); 
    widgetMap.put("widget",memberMap); 

    try { 
     document.putProperties(widgetMap); 
    } catch (CouchbaseLiteException e) { 
     Log.e("", "Cannot save document", e); 
    } 
+0

感謝您的回答。我在運行時也是這樣做的。我如何檢索這個。請在答案中添加該代碼。 –

+0

你需要檢索它通過使用查看或所有文檔查詢,btw請將其標記爲答案 –

+0

隊友實際上你做靜態我需要動態追加。請更新答案。 –

0

對Couchbase Lite中文檔的約束是_id屬性應該是唯一的。隨着文檔更新,它會創建新的修訂版。

有兩種可能的方法來更新文檔:

  • putProperties:給定一個新的JSON對象,它取代了文件的身體與該對象。
  • update:需要回調函數或塊。它加載當前版本的屬性,然後調用該函數,並傳遞一個UnsavedRevision對象,該對象的屬性是當前版本的可變副本。你的回調代碼可以修改這個對象的屬性,在它返回之後,修改後的版本將被保存併成爲當前版本。

因此,更新與image字典中的窗口小部件文檔,你應該使用更新方法:

final Map<String, Object> imageProperties = new HashMap<String, Object>(); 
imageProperties.put("src", "Images/Sun.png"); 
imageProperties.put("name", "sun1"); 
// ... 

Document document = database.getDocument(widgetId); 
document.update(new Document.DocumentUpdater() { 
    @Override 
    public boolean update(UnsavedRevision newRevision) { 
     Map<String, Object> properties = newRevision.getUserProperties(); 
     properties.put("image", imageProperties); 
     newRevision.setUserProperties(properties); 
     return true; 
    } 
}); 

注:建議使用圖書館像傑克遜序列化/反序列化JSON和POJO Java應用程序中的模型(您可以閱讀此blog post以查找更多信息)。

0

我遇到了同樣的問題。您需要遍歷json的鍵並使用putproperties添加每個對象。但是,對於JSONArray,您需要使用ArrayList。我遍歷使用傑克遜庫(這也被用於couchbase內部)鍵

 try { 
      Map<String, Object> properties = new HashMap<String, Object>(); 
      JsonFactory factory = new JsonFactory(); 
      ObjectMapper mapper = new ObjectMapper(factory); 

      // convert you json string to Jackson JsonNode 
      JsonNode rootNode = mapper.readTree(doc.toString()); 

      Iterator<Map.Entry<String, JsonNode>> it = rootNode.getFields(); 
      while (it.hasNext()) { 
       Map.Entry<String, JsonNode> pair = it.next(); 
       String key = pair.getKey().toString(); 
       String value = pair.getValue().toString(); 
       if (JSONDiffUtil.isJSONArray(value)) { 
        Debug.logInfo("its a json array: " + key, MODULE); 
        ArrayNode arrNode = (ArrayNode) pair.getValue(); 
        properties.put(key, arrNode); 
       } 
       else if (key.startsWith("_")) { 
        Debug.logInfo("skipping _rev" + key, MODULE); 
       } 
       else { 
        Debug.logInfo("its a json object: " + key, MODULE); 
        properties.put(key, pair.getValue()); 
       } 

     } 
      document.putProperties(properties); 
     } 

     catch (CouchbaseLiteException e) { 
      Debug.logInfo("Error", MODULE); 
      Debug.logInfo(e.getMessage(), MODULE); 
     } catch (JsonProcessingException e) { 
      Debug.logInfo(e.getMessage(), MODULE); 
     } catch (IOException e) { 
      Debug.logInfo(e.getMessage(), MODULE); 
     } 
相關問題