英文:
BIRT Report JSON array printed values
问题
我试图将数据库列中的JSON值打印到我的Birt报表中的多行中。它可以很好地打印出JSON数组的最后一行,但之前的行却不能打印出来。
我的动态文本字段的代码:
var phone = JSON.parse(row["c_numbers"]);
for(var k in phone) {
phone[k]['type']+': '+phone[k]['phone']
}
这是它打印出来的内容:
[![enter image description here][1]][1]
这是数据库中的JSON数据:
[{"type": "Cell", "phone": "123-123-1233"}, {"type": "", "phone": "123-423-4123"}]
最简单的方法就是将其打印出来,但它只能打印出最后一个数组项。报表设计:
[![enter image description here][2]][2]
所以它能够获取数据,遍历JSON,并进行打印,但只有最后一个数组项被打印出来。
[1]: https://i.stack.imgur.com/MDSEc.png
[2]: https://i.stack.imgur.com/70Zg5.png
英文:
I'm trying to print out JSON values from a database column into multiple lines in my Birt report. It prints the last line of the JSON array just fine, but not the lines before it.
My code for my dynamic text field:
var phone = JSON.parse(row["c_numbers"]);
for(var k in phone) {
phone[k]['type']+': '+phone[k]['phone']
}
This is what it prints:
And here is the JSON from the database:
[{"type": "Cell", "phone": "123-123-1233"}, {"type": "", "phone": "123-423-4123"}]
Easiest would be just to print it out, but it's not doing more than the last array item. Report design:
So it's getting the data, iterating over the JSON, and printing, but not all array items, only the last one.
答案1
得分: 0
答案是将新数据分配给一个新变量,然后在最后打印出该最终变量。我还必须在每个输出末尾添加`<br>`来进行换行。
var phone = JSON.parse(row["c_numbers"]);
var finalPhone = '';
for(var k in phone) {
finalPhone = finalPhone + (phone[k]['type'] > '' ? phone[k]['type'] + ': ' : '') + phone[k]['phone'] + "<br>";
}
finalPhone;
英文:
Answer was to assign the new data to a new variable, then print out that final variable at the end. I also had to add <br>
for a newline at the end of each output.
var phone = JSON.parse(row["c_numbers"]);
var finalPhone = '';
for(var k in phone) {
finalPhone = finalPhone+(phone[k]['type'] > '' ? phone[k]['type']+': ' : '')+phone[k]['phone']+"<br>"
}
finalPhone;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论