编程语言
首页 > 编程语言> > Java-Birt报告中的嵌套循环

Java-Birt报告中的嵌套循环

作者:互联网

我正在使用Eclipse Birt从JSON文件生成报告.

我的JSON文件如下所示:

{  
"cells":[  
  {  
     "type":"basic.Sensor",
     "custom":{  
     "identifier":[  
           {  
              "name":"Name1",
              "URI":"Value1"

           },
           {  
              "name":"Name4",
              "URI":"Value4"
           }
        ],
        "classifier":[  
           {  
              "name":"Name2",
              "URI":"Value2"
           }
        ],
        "output":[  
           {  
              "name":"Name3",
              "URI":"Value3"
           }
        ],

     },
           "image":{  
           "width":50,
           "height":50,
           "xlink:href":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAABEJAAARCQBQGfEVAAAABl0RVh0U29mdHdhcmUAd3Vi8f+k/EREURQtsda2Or/+nFLqP6T5Ecdi0aJFL85msz2Qxyf4JIumMAx/ClmWt23GmL1kO54CXANAVH+WiN4Sx7EoNVkU3Z41BDHMeXAxjvOxNr7RJjzHX7S/jAflwBxkJr/RwiOpWZ883Nzd+Wpld7tkBr/SJr7ZHZbHZeuVweSnPfniocMAWYwcGBafH0OoPamFGAaY4ZBZjmmFGAaY4ZBZjmmFGAaY4ZBZjmmFGAaY7/B94QnX08zxKLAAAAAElFTkSuQmCC"
        }
     }
  },
  {  
     "type":"basic.Sensor",

     "custom":{  
        "identifier":[  
           {  
              "name":"Name1",
              "URI":"Value1"

           },
           {  
              "name":"Name4",
              "URI":"Value4"
           }
        ],
        "classifier":[  
           {  
              "name":"Name2",
              "URI":"Value2"
           }
        ],
        "output":[  
           {  
              "name":"Name3",
              "URI":"Value3"
           }
        ],

     },

        "image":{  
           "width":50,
           "height":50,
           "xlink:href":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9igAQAAAgAIoAEAAAIACKABAAACAAigAQAAAgAIoAEAAAIACKABAAACAAigAQAAAgAIoAEAAAIADqhvprADeSsau00l5NAAAAAElFTkSuQmCC"
        }
     }
  },
  {  
     "type":"basic.Platform",

     "custom":{  
        "identifier":[  
           {  
              "name":"Name1",
              "URI":"Value1"

           }
        ],
        "classifier":[  
           {  
              "name":"Name2",
              "URI":"Value2"
           }
        ],
        "output":[  
           {  
              "name":"Name3",
              "URI":"Value3"
           }
        ],

        "image":{  
           "width":50,
           "height":50,
           "xlink:href":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAA6TH0jqtg6U8EsCdnm3SpevSK7Pb85xABEMBuLAn2hxjRve7SFzYEaB/HhytLQ4ABRwCWBPvBKnRk6U8EkBeOD9f7iwAGHAGEYEmwDxLvzNKfCCDP8NGLQd3lY7D0JwI4kmlwfHhX6dTSXxsRAAHsWR7aUjc7uM5Wg=="
        }
     }
  }
]
 }

我有3个单元格,每个单元格包含1个图像1个名称1个类型和3个表,这是我到目前为止所做的:

我正在努力做的是一个嵌套循环,我想为JSON中的每个object(Cell)都加上一个这样的段落:

> 2.x组件单元的名称:

>图片
>输出表
>标识符表
>分类表

因此,要做到这一点,我需要在每个单元格上进行迭代,然后在每个表上进行迭代,输出,标识符和分类器,而且我不知道如何执行此操作,即嵌套循环.类似于表示单元格数量的列表,其中包含3个表,一个图像和一个名称.

**编辑:**
这是数据集的开放方法

// Grab the JSON file and place it in a string
fisTargetFile = new FileInputStream(new File("C:/Users/Sample Reports/moe.json"));
input = IOUtils.toString(fisTargetFile, "UTF-8");

// Store the contents in a variable
jsonData = input;

// Convert the String to a JSON object
myJSONObject = eval( '(' + jsonData + ' )' );

// Get the length of the object
len = myJSONObject.cells.length;

// Counter
count = 0;

提取方法:

if(count < len) {
var name     = myJSONObject.cells[count].attrs.text["text"];
var type    = myJSONObject.cells[count].type;
var icon =myJSONObject.cells[count].attrs.image["xlink:href"];



icon = icon.split(",");

icon= icon[1];
imageDataBytes = icon;

row["name"]     = name;
row["type"]    = type;
row["icon"]    = Base64ToBlob.toBytes(icon);



Logger.getAnonymousLogger().info( row["icon"]);
count++;
return true;
}

return false;

解决方法:

您想使用嵌套表,有一个很好的教程,展示了如何将嵌套表链接到外部表:请先watch carefully this demo,尤其要了解如何通过数据集参数将子表链接到外部表.

当然,您的案例更具挑战性,因为您需要使用脚本化数据集和多个子表来执行此操作.我已经做了类似的事情,您必须为每个子表创建一个脚本化数据集.关键点是:

>在每个子数据集的“参数”部分中,创建一个输入参数并将其命名为实例“ systemID”
>通过“拖放”外部表中的每个数据集来创建子表
>在每个子表的“绑定”部分中,将参数“ systemID”链接到外部表的ID字段
>在子数据集的“打开”事件中,使用以下表达式访问参数的值:inputParams [“ systemID”]这样就可以过滤“ myJSONObject”中的相关行.
>确保将“ myJSONObject”全部初始化一次非常重要,否则,如果在每次迭代中对其进行评估,性能可能会大大降低.例如,在报告的“初始化”事件中对其进行评估.

就是这样,这并不容易,但是这些要素应该有助于实现此报告.

标签:birt,reporting,json,java,birt-deapi
来源: https://codeday.me/bug/20191120/2042632.html