2017-04-11 90 views
0

我有以下視圖無法檢索JSON值

{ 
    "views" : { 
    "categories" : { 
     "map" : "function (doc) { emit(doc._id,{"title":doc.title,"parentnode":doc.parentnode});}" 
    } 
    } 
} 

即,對於每個文件,返回一個JSON對象,具有兩個密鑰:titleparentnode與它們各自的值。視圖運行在cloudant UI

{ 
"id": "3bacce314363eb954f1922ff3cd2240c", 
"key": "3bacce314363eb954f1922ff3cd2240c", 
"value": { 
    "title": "poi", 
    "parentnode": "asd" 
}, 
"_id": "3bacce314363eb954f1922ff3cd2240c" 
} 

這是完美就好了。現在,我嘗試在我的Java程序作爲

List<JSONObject> vals = cloudant.getViewRequestBuilder("categoryDesign", "categories") 
.newRequest(com.cloudant.client.api.views.Key.Type.STRING, JSONObject.class) 
.includeDocs(true) 
.build() 
.getResponse() 
.getValues(); 

注意JSONObject在這種情況下org.json.JSONObject;閱讀本。但是,爲了這個,我得到

[{}] 

所以後來我改變了看法有點

{ 
     "views" : { 
     "categories" : { 
      "map" : "function (doc) { emit(doc._id,doc.title+":"+doc.parentnode);}" 
     } 
     } 
    } 

,並在cloudant UI我看到

{ 
"id": "9db1f03e8f4d239a6e18d4612b1a4275", 
"key": "9db1f03e8f4d239a6e18d4612b1a4275", 
"value": "poi:asd", 
"_id": "9db1f03e8f4d239a6e18d4612b1a4275" 
} 

,現在我做

List<String> vals = cloudant.getViewRequestBuilder("categoryDesign", "categories") 
.newRequest(com.cloudant.client.api.views.Key.Type.STRING, String.class) 
.includeDocs(true) 
.build() 
.getResponse() 
.getValues(); 

現在,輸出是

["poi:asd"] 

我能做些什麼來讀取值爲JSONObject s?

跟進:如何從視圖的輸出中刪除重複項?

回答

1

Cloudant客戶端似乎不能與org.json.JSONObject一起使用。我得到你的第一個例子與com.google.gson.JsonObjectorg.apache.wink.json4j.JSONObject一起工作。下面是Maven依賴:

com.google.gson.JsonObject:

<dependency> 
    <groupId>com.google.code.gson</groupId> 
    <artifactId>gson</artifactId> 
    <version>2.7</version> 
</dependency> 

org.apache.wink.json4j.JSONObject:

<dependency> 
    <groupId>org.apache.wink</groupId> 
    <artifactId>wink-json4j</artifactId> 
    <version>1.4</version> 
</dependency> 

輸出System.out.println(vals.toString())

[{"parentnode":"asd","title":"poi"}] 

要回答後續問題:

您可以通過在視圖中使用reduce函數(sum或count)來消除重複項,但這也需要您將密鑰更改爲數據的唯一密鑰。所以,如果標題和parentnode的組合是你想成爲你的唯一關鍵是什麼,你的看法是這樣的:

"categoriesNoDups": { 
    "reduce": "_sum", 
    "map": "function (doc) { emit([doc.title, doc.parentnode], 1); } }" 
} 

現在,當你打電話給你的視圖(注意新視圖命名爲categoriesNoDups)你要添加?group=true像這樣:

https://youraccount.cloudant.com/yourdb 
/_design/categoryDesign/ 
_view/categoriesNoDups?group=true 

的數據將類似於以下內容:

{ 
    "rows": [ 
    { 
     "key": [ 
     "poi", 
     "asd" 
     ], 
     "value": 2 
    } 
    ] 
} 

現在,而不是得到的值喲你想得到鑰匙。要在Java中檢索密鑰,您應該這樣做:

List<Key.ComplexKey> keys = cloudant.getViewRequestBuilder("categoryDesign", "categoriesNoDups") 
    .newRequest(Key.Type.COMPLEX, Number.class) 
    .group(true) 
    .build() 
    .getResponse() 
    .getKeys(); 
    for (Key.ComplexKey key : keys) { 
     JSONArray keyValues = new JSONArray(key.toJson()); 
     System.out.println("title = " + keyValues.getString(0)); 
     System.out.println("parentnode = " + keyValues.getString(1)); 
    } 

現在您又回到處理數組而不是JSON對象。另請注意:我使用Apache Wink JSON庫將鍵轉換爲JSON對象(它們只是數組),然後訪問這些對象的值。輸出看起來類似於以下內容:

title = poi 
parentnode = asd 
+0

謝謝!那工作。關於後續的任何想法? – AbtPst

+0

查看最新答案 – markwatsonatx

+0

非常感謝!這樣可行 – AbtPst