如何将_design视图结果解析为Java对象(getMethods())?
请问有谁知道如何将查询结果元素解析为Java Objects而不是使用声明的String?可能吗?我在这里找到的一些建议使用了一个声明的String,它不适合我的概念。
我的文档结构如下:
{
"_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":]}
resultAdHoc2
的输出它在设计视图中返回了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}"}}}
答案 0 :(得分: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());