简体   繁体   English

使用javax.json从列表创建json

[英]create json from list with javax.json

I have a List with 3000 items and I have to create a json from it and send it to my client side code which is Extjs. 我有一个包含3000个项目的列表,我必须从中创建一个json并将其发送到我的客户端代码Extjs。 As JsonArrayBuilder does not accept list, I'm iterating through the list and add create each json object and finally send it to the client side. 由于JsonArrayBuilder不接受列表,因此我遍历该列表并添加创建每个json对象,最后将其发送到客户端。 The problem is that this process takes 13 seconds which is really high. 问题在于此过程需要13秒,这确实很高。

Here is the way I make this json array. 这是我制作此json数组的方法。

 JsonObjectBuilder builder = Json.createObjectBuilder();
 JsonArrayBuilder childrenArrayBuilder = Json.createArrayBuilder();
        builder.add("success", true);
        builder.add("children, childrenArrayBuilder);

here is how I iterate through the list 这是我遍历列表的方式

           for (TagDefinitionProject tt : myList) {

            childrenArrayBuilder.add(
                    Json.createObjectBuilder()
                    .add("id", getTreeNodeId(tt.getTag()) + "-" + (idSplit[1]))
                    .add("nodeStatus", tt.getStatus())
                    .add("text", tt.getNJTagName())
                    .add("tagNJName", tt.getTag().getName())
                    .add("baseType", tt.getTag().getBaseType().getName())
                    .add("definitionType", def)
                    .add("leaf", (tt.getTag().getChildren().size() == 1 || tt.getTag().getChildren().get(1).getChild().equals(tt.getTag()))));
            }

How can improve the performance here? 如何改善此处的性能?

The preferred method of creating multiple builders with the javax Json library is by using JsonBuilderFactory. 使用javax Json库创建多个构建器的首选方法是使用JsonBuilderFactory。 I've modified your code below. 我已经在下面修改了您的代码。

     // Create a new factory. The argument, config, can be null or
     // contain a Map with configuration values for the created builders.
     JsonBuilderFactory factory = Json.createBuilderFactory(config);
     JsonObjectBuilder builder = factory.createObjectBuilder();
     JsonArrayBuilder childrenArrayBuilder = factory.createArrayBuilder();
            builder.add("success", true);
            builder.add("children", childrenArrayBuilder);


               for (TagDefinitionProject tt : myList) {

                childrenArrayBuilder.add(
                        factory.createObjectBuilder()
                        .add("id", getTreeNodeId(tt.getTag()) + "-" + (idSplit[1]))
                        .add("nodeStatus", tt.getStatus())
                        .add("text", tt.getNJTagName())
                        .add("tagNJName", tt.getTag().getName())
                        .add("baseType", tt.getTag().getBaseType().getName())
                        .add("definitionType", def)
                        .add("leaf", (tt.getTag().getChildren().size() == 1 || tt.getTag().getChildren().get(1).getChild().equals(tt.getTag()))));
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM