您好我想从Note视图创建一个带有值的JSON字符串。我希望在视图中获取所有主要文档,然后以递归方式获取它们的响应,然后以JSON字符串形式返回。
{identifier: 'name',
label: 'name',
items: [
{"name": "Africa", "": "continent", "children": "[
{ "name":"Egypt", "field":"country" },
{ "name":"Kenya", "field":"country", "children":"[
{ "name":"Nairobi", "field":"city" },
{ "name":"Mombasa", "field":"city" } ]"
]},
{ "name":"Sudan", "field":"country", "children":"[
{ "name":'Khartoum', "field":"city" }]"
},
{ "name":'Asia', "field":"continent", "children":"[
{ "name":"China", "field":"country" },
{ "name":"India", "field":"country"},
{ "name":"Russia", "field":"country" },
{ "name":"Mongolia", "field":"country" } ]"
}
}
]}
我尝试过以下代码,只打印出主文档而不是儿童的回复:
public String getJson() {
Database db = null;
Document temDoc = null;
JSONObject json = new JSONObject();
try {
View view1 = database.getView("view1");
Document doc = view1.getFirstDocument();
ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
while (doc != null) {
String name =doc.getItemValueString("Name");
String field = doc.getItemValueString("FieldValue");
DocumentCollection respDoc =doc.getResponses();
if(!(respDoc.getCount() > 0)){
result.add(splitHash(name,field));
}else{
result.add(getResp(name,field,respDoc));
}
temDoc = view1.getNextSibling(doc);
doc.recycle();
doc =temDoc;
}
json.put("identifier", "name");
json.put("label", "name");
json.put("items", result);
return json.toJSONString();
}
private HashMap<String, String> getResp(String name, String field,DocumentCollection respDoc) throws NotesException {
HashMap<String, String> child = new HashMap<String, String>();
HashMap<String, String> hmValue = new HashMap<String, String>();
Document tmpDoc;
String field ="";
try {
Document doc = respDoc.getFirstDocument();
while (doc != null) {
name =doc.getItemValueString("Name");
field = doc.getItemValueString("FieldValue");
DocumentCollection responses = doc.getResponses();
if(!(responses.getCount() > 0)){
hmValue.put(name, name);
hmValue.put(field, field);
}else{
getResp(name,field,responses);
hmValue.put("children",hmValue.toString());
}
tmpDoc = respDoc.getNextDocument(doc);
doc.recycle();
doc =tmpDoc;
}
child.addAll(hmValue);
return child;
}
private HashMap<String, String> splitHash(String name, String field) {
HashMap<String, String> hm = new hm<String, String>();
hm.put("name", name);
hm.put("field", field);
return hm;
}
答案 0 :(得分:1)
使用JSONObject和JSONArray从视图中的主要文档及其响应创建分层JSON结构:
public String getJson() throws NotesException {
...
View view1 = ...;
JSONObject jsonMain = new JSONObject();
jsonMain.put("identifier", "name");
jsonMain.put("label", "name");
Document doc = view1.getFirstDocument();
JSONArray items = new JSONArray();
while (doc != null) {
items.add(getJsonDocAndChildren(doc));
Document docTemp = view1.getNextSibling(doc);
doc.recycle();
doc = docTemp;
}
jsonMain.put("items", items);
return jsonMain.toJSONString();
}
private JSONObject getJsonDocAndChildren(Document doc) throws NotesException {
JSONObject jsonDoc = new JSONObject();
jsonDoc.put("name", doc.getItemValueString("Name"));
jsonDoc.put("field", doc.getItemValueString("FieldValue"));
DocumentCollection responses = doc.getResponses();
if (responses.getCount() > 0) {
Document docResponse = responses.getFirstDocument();
JSONArray children = new JSONArray();
while (docResponse != null) {
children.add(getJsonDocAndChildren(docResponse));
Document docTemp = responses.getNextDocument(docResponse);
docResponse.recycle();
docResponse = docTemp;
}
jsonDoc.put("children", children);
}
return jsonDoc;
}
答案 1 :(得分:0)
请参阅XPages帮助应用程序(http://xhelp.openntf.org),它类似于构建左侧导航。但是,凭借我现在的知识,我将更改为使用JSONJavaObject类来最小化由创建字符串而不是让预构建的类进行转换所导致的开发人员错误。但是有什么效果很好。