BIRT报告中JSON数组打印的数值

huangapple go评论98阅读模式
英文:

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:

BIRT报告中JSON数组打印的数值

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:

BIRT报告中JSON数组打印的数值

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 &lt;br&gt; for a newline at the end of each output.

var phone = JSON.parse(row[&quot;c_numbers&quot;]);
var finalPhone = &#39;&#39;;

for(var k in phone) {
    finalPhone = finalPhone+(phone[k][&#39;type&#39;] &gt; &#39;&#39; ? phone[k][&#39;type&#39;]+&#39;: &#39; : &#39;&#39;)+phone[k][&#39;phone&#39;]+&quot;&lt;br&gt;&quot;
}

finalPhone;

huangapple
  • 本文由 发表于 2020年8月29日 01:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63638580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定