我想使用Java从分类视图创建一个Json String,但是我无法让Json子类保持在父级之下。我保持以下输出:
{“identifier”:“id”,“label”:“State”,“items”:[
会知道从2级分类视图生成JSON字符串是否可行,或者是否有更好的方法来实现它。
下面是我的java方法:
public ResponseWriter getJSONData() {
long startTime = System.currentTimeMillis();
FacesContext facesContext = FacesContext.getCurrentInstance();
ResponseWriter resWriter = facesContext.getResponseWriter();
//StringWriter stringOut = new StringWriter();
JsonWriter writer = new JsonWriter(resWriter,false);
Database db = null;
try {
writer.startObject();
writer.startProperty("identifier");
writer.outStringLiteral("id");
writer.endProperty();
writer.startProperty("label");
writer.outStringLiteral("State");
writer.endProperty();
writer.startProperty("items");
writer.startArray();
db = DominoUtils.getCurrentSession().getCurrentDatabase();
View lookupDB = db.getView("StateAndCity");
//Document doc = lookupDB.getFirstDocument();
ViewNavigator nav = lookupDB.createViewNav();
ViewEntry ve = nav.getFirst();
boolean run =false;
while (ve != null){
counter++;
Vector cv = ve.getColumnValues();
int level = ve.getIndentLevel();
String levelTitle = (String) cv.get(level);
writer.startArrayItem();
writer.startObject();
if (ve.isCategory()) {
if (run) {
writer.endArrayItem();
writer.endArray();
}
run= true;
writer.startArrayItem();
writer.startProperty("id");
writer.outStringLiteral(getID());
writer.endProperty();
writer.startProperty("title");
writer.outStringLiteral(levelTitle);
writer.endProperty();
writer.startProperty("category");
writer.outStringLiteral("true");
writer.endProperty();
writer.startProperty("children");
writer.startArray();
}else{
writer.startArrayItem();
writer.startObject();
Document doc = ve.getDocument();
writer.startProperty("id");
writer.outStringLiteral(getID());
writer.endProperty();
writer.startProperty("docId");
writer.outStringLiteral(doc.getUniversalID());
writer.endProperty();
writer.startProperty("title");
writer.outStringLiteral(levelTitle);
writer.endProperty();
writer.startProperty("category");
writer.outStringLiteral("false");
writer.endProperty();
writer.endObject();
writer.endArrayItem();
}
ViewEntry tmpentry = nav.getNext();
ve.recycle();
ve = tmpentry;
}
writer.endArray();
writer.endProperty();
writer.endObject();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
//System.out.println("running time: "+totalTime);
writer.flush();
return resWriter;
}catch (NotesException e) {
e.printStackTrace();
}
return resWriter;
}
更新 想使用我的返回JSON来创建以下类型的网格,但具有两个级别类别State和City
JSON输出
{
"identifier": "id",
"label": "name",
"items": [{
"id": "AK",
"type": "state",
"state": "AK",
"childItems": [{
"id": "Anchorage",
"type": "city",
"city": "Anchorage",
"numPeople": "2",
"childItems": [{
"id": "B3093953178C98E905257838007ABC48",
"firstname": "Bella",
"lastname": "Martin",
"valueToAdd": "2"
},
{
"id": "7FDB9CCDE7D6923E05257838007ABC1E",
"firstname": "Brian",
"lastname": "Leggett",
"valueToAdd": "2"
}
]
}]
....
}]
}
答案 0 :(得分:1)
你可以回归“旧的”多米诺骨牌技巧并取消所有Java。将视图定义为Passthru HTML并使用mime类型“text / Json”创建“$$ ViewTemplate for ViewName”表单。然后,您可以使用?Openview访问输出。根据需要喷洒Json标记。
或者只使用... / viewname?ReadviewEntries& Outputformat = json
构建Json输出
答案 1 :(得分:1)
看起来您的代码在写入任何内容之前就崩溃了。有一些问题陈述:
int level = ve.getIndentLevel();
不是,所以你最有可能得到索引超出范围的错误String levelTitle = (String) cv.get(level);
更改为String levelTitle = String.valueOf(cv.get(level));
我建议您将方法签名更改为public String getJSONFromView(final Database db, final String viewName) {...}
之类的内容然后使用一些包装代码来调用该方法。这允许您在Domino命令行应用程序中使用该类,这在本地的Designer / Eclipse中进行调试非常方便。由于该类没有XPage依赖,使生活更轻松。我开发了所有VoP:核心没有服务器,只有Notes类依赖。您将创建一个这样的测试类(省略错误处理):
public class Test1 {
public static void main(String args[]) {
NotesThread.sinitThread();
Session s = NotesFactory.createSession();
Database db = s.openDatabase("someserver","somedb");
Yourclass yc = new Yourclass();
System.out.println(yc.getJSONFromView(db,"viewname");
db.recycle();
s.recycle();
NotesThread.stermThread();
}
}
您可能需要在Domino设计器中切换到Java透视图并创建一个标准Java项目。这应该允许您在XPage中使用Java类之前使其成为您想要的。
我写了类似的东西,你可以check it out。
让我们知道需要澄清的内容。