Java XPages:递归创建JSON字符串

Java XPages:递归创建JSON字符串,java,json,recursion,xpages,Java,Json,Recursion,Xpages,您好,我想创建一个JSON字符串,该字符串的值来自Notes视图。我希望在一个视图中获取所有主要文档,然后递归地获取它们的响应,这些响应将作为JSON字符串返回 {identifier: 'name', label: 'name', items: [ {"name": "Africa", "": "continent", "children": "[ { "name":"Egypt", "field":"country" }, { "n

您好,我想创建一个JSON字符串,该字符串的值来自Notes视图。我希望在一个视图中获取所有主要文档,然后递归地获取它们的响应,这些响应将作为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;
}
公共字符串getJson(){
数据库db=null;
单据temDoc=null;
JSONObject json=新的JSONObject();
试试{
View view1=database.getView(“view1”);
Document doc=view1.getFirstDocument();
ArrayList结果=新建ArrayList();
while(doc!=null){
字符串名称=doc.getItemValueString(“名称”);
字符串字段=doc.getItemValueString(“FieldValue”);
DocumentCollection respDoc=doc.getResponses();
如果(!(respDoc.getCount()>0)){
添加(splitHash(名称、字段));
}否则{
添加(getResp(名称、字段、respDoc));
}
temDoc=view1.getNextSibling(doc);
doc.recycle();
doc=temDoc;
}
put(“标识符”、“名称”);
put(“标签”、“名称”);
json.put(“项”,结果);
返回json.toJSONString();
}
私有HashMap getResp(字符串名称、字符串字段、DocumentCollection respDoc)引发NotesException{
HashMap child=新的HashMap();
HashMap hmValue=新的HashMap();
文件tmpDoc;
字符串字段=”;
试一试{
Document doc=respDoc.getFirstDocument();
while(doc!=null){
名称=doc.getItemValueString(“名称”);
field=doc.getItemValueString(“FieldValue”);
DocumentCollection responses=doc.getResponses();
如果(!(responses.getCount()>0)){
hmValue.put(名称、名称);
hmValue.put(字段,字段);
}否则{
getResp(名称、字段、响应);
hmValue.put(“children”,hmValue.toString());
}
tmpDoc=respDoc.getNextDocument(doc);
doc.recycle();
doc=tmpDoc;
}
addAll(hmValue);
返回儿童;
}
私有HashMap splitHash(字符串名称、字符串字段){
HashMap hm=新的hm();
hm.put(“名称”,名称);
hm.put(“字段”,字段);
返回hm;
}

请参阅XPages帮助应用程序(),它的功能类似于构建左侧导航。不过,根据我现在掌握的知识,我将更改为使用JSONJavaObject类,以最大限度地减少由于创建字符串而导致的开发人员错误,而不是让预构建的类进行转换。但是有什么功能可以很好地工作。

使用JSONObject和JSONArray到c从视图中的主要文档及其响应创建层次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;
}

哦,谢谢,我已经在上面对它进行了调整,但仍然是一样的,它只给出了主文档,没有响应。你的问题中的代码真的是你正在处理的代码吗?编译器应该抱怨方法getResp()中的返回值
child
)因为它的类型与方法定义不同…是的,我在我的系统上编辑了它,但忘了更新行您的
HashMap
方法不会成功。它必须是分层的。我为您创建了一个应该可以工作的解决方案。在我的打印中,我注意到项目[]首先打印的是标识符“name”和标签“name”,这对于XPages是正常的还是排序问题?