gpt4 book ai didi

java - XPage:从分类 View 创建 Json 字符串

转载 作者:行者123 更新时间:2023-12-02 02:38:10 25 4
gpt4 key购买 nike

我想使用 Java 从分类 View 创建一个 Json 字符串,但我无法使 Json 子类别保留在父类别下。我不断收到以下输出:

{ "identifier":"id", "label":"State", "items": [

想知道是否可以从 2 级分类 View 生成 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 创建一个以下类型的网格,但具有两级类别“州”和“城市”

enter image description here

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"
}
]
}]

....
}]
}

最佳答案

看起来您的代码在写入任何内容之前就崩溃了。有一些有问题的说法:

  • vector/数组是从零开始的,但 int level = ve.getIndentLevel(); 不是,因此您很可能会遇到索引越界错误
  • 如果返回 null,则转换为 String 也会出错。因此将 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 .

让我们知道什么需要澄清。

关于java - XPage:从分类 View 创建 Json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45940730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com