Reputation: 403
Hello i would like to creat a JSON String with value from Note view. I want to get all the main documents in a view, then gets their responses recursively which will then be return as JSON String.
{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" } ]"
}
}
]}
I have tried the below code which only print out the main document and not the responses which are the children:
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;
}
Upvotes: 2
Views: 473
Reputation: 30970
Work with JSONObject and JSONArray to create a hierarchical JSON structure from main documents in view and their responses:
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;
}
Upvotes: 2
Reputation: 15739
See XPages Help Application (http://xhelp.openntf.org), which does something similar to build the left-hand navigation. With the knowledge I have now, though, I would change the to use JSONJavaObject class to minimise developer errors caused by creating strings rather than letting prebuilt classes do the conversion. But what is there works perfectly well.
Upvotes: 0