1

如何将 _design 视图结果解析为 Java 对象 (getMethods())?

请问有谁知道我如何将查询结果元素解析为 Java 对象而不是使用声明的字符串?可能吗?我在这里找到的几个建议使用了不适合我的概念的声明字符串。

我的文档结构如下:

{
   "_id": "37636ec5fc94e8e60a2744720e001441",
   "_rev": "1-cc97f6f67206667466d619901fb4eb8b",
   "CREATED_DATE": "19-09-13",
   "MODIFICATION_DATE": "20-09-13",
   "VERSION": "1.0",
   "VISIBILITY": "True",
   "PRICE_LIST_ID": "00100",
   "MODIFICATION_USER": "IKK",
   "CUSTOMER_ID": "02227802",
   "CREATION_USER": "IKK",
   "PDF_FILE_NAME": "KOOO",
   "DELETION_FLAG": "1",
   "PDF_FILE_CONTENT": "Pricelist"
 }

我的 _design 文档是:_design/VIEW_ALL_DOCS

function(doc){
if(doc.DELETION_FLAG == "1" ){
  emit(doc.DELETION_FLAG, doc);
 }
}

以下是我用于应用程序的视图查询

ViewResults resultAdHoc2 = DbProperties.db.view("_design/VIEW_ALL_DOCS"); // 2
ViewResults resultAdHoc = DbProperties.db.view("_all_docs"); // 1

Gson gson = new Gson();

    PriceListDocument pld = gson.fromJson(resultAdHoc.toString(), PriceListDocument.class);     
    System.out.println("id: "+pld.getPriceListId()+" Document Name:"+pld.getDocFileName()  
               +" Original File Name: "+pld.getOriginalFileName()+" Deletion Flag: "+pld.isDeletionFlag() 
               +" Doc FileContents: "+pld.getDocFileContents() +" Visibility: "+pld.getVisibility()
               +" Doc File Version: "+pld.getDocFileVersion() +" Creation Date: "+pld.getCreationDate() 
               +" Creation User: " +pld.getCreationUser() +" Modification Date "+pld.getModificationDate() 
               +" Modification User "+pld.getModificationUser()); 

控制台输出如下:

id: null Document Name:null Original File Name: null Deletion Flag: false Doc FileContents: null Visibility: null Doc File Version: 0 Creation Date: null Creation User: null Modification Date null Modification User null

的输出resultAdHoc低于它仅返回没有元素的文档标题

{"total_rows":9,"offset":0,"rows":    [{"id":"0ecb06ce81df89c03dbedecf47001b4b","key":"0ecb06ce81df89c03dbedecf47001b4b","value":
{"rev":"2-67d92be4f768a6d91f4f4196a264897e"}},{"id":"0ecb06ce81df89c03dbedecf47002a89","key":"0ecb06ce81df89c03dbedecf47002a89","value"
:{"rev":"1-7a0c8243e56157ae1d71d3a63c49e590"}},    {"id":"0ecb06ce81df89c03dbedecf47002b81","key":"0ecb06ce81df89c03dbedecf47002b81","value":]}

resultAdHoc2is It的输出返回设计视图中的 javascript 函数,但不返回符合条件的文档。

{"_id":"_design/VIEW_ALL_DOCS","_rev":"1-8a5809780cead5e4747fb0e53ebca081","language":"javascript","views":{"VIEW_ALL_DOCS":{"map":"function(doc){\nif(doc.DELETION_FLAG == \"0\" ){\n  emit(doc.DELETION_FLAG, doc);\n }\n}"}}}
4

1 回答 1

0

我不太确定您的要求,我尽量根据您提供的标签做出最好的猜测。

我的理解是,您想访问来自某个地方的 JSON 字符串的元素。为此,您需要将字符串反序列化为 POJO,然后访问其元素。

像这样声明你的 POJO Document

public class Document {
   String _id;
   int DELETION_FLAG;
   // more member variables...

   public int getDeletionFlag{
      return DELETION_FLAG;
   }
   // more getters/setters

}

接着:

String json = "{\"_id\": \"37636ec5fc94e8e60a2744720e001441\", ....."; // put here your string
Gson g = new Gson();
Document test1 = g.fromJson(json, Document.class);
System.out.println(test1.getDeletionFlag());
于 2013-10-23T06:13:00.480 回答