首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Groovy脚本将json转换为html

如何使用Groovy脚本将json转换为html
EN

Stack Overflow用户
提问于 2018-07-06 23:34:35
回答 2查看 3.4K关注 0票数 0

正如标题所暗示的,我正在尝试使用Groovy将json转换为html。我知道如何在python中做到这一点,但是因为这段代码是在jenkins中运行的,所以我需要找到一种使用Groovy的方法。

这个json :

代码语言:javascript
复制
[
{
    "kubernetes.pod.name": "sds-endpoints-6-hn0fe2l",
    "container.id": "d19e001824978",
    "memory.used.percent": 102,
    "cpu.used.percent": 7,
    "memory.bytes.used (mB)": 2067,
    "cpu.cores.used (millicores)": 9,
    "endTime": "2018-07-04T02:00:00+0000"
},
{
    "kubernetes.pod.name": "product-service-endpoints-4-da1w",
    "container.id": "4dd6447f5e14",
    "memory.used.percent": 84,
    "cpu.used.percent": 7,
    "memory.bytes.used (mB)": 1698,
    "cpu.cores.used (millicores)": 8,
    "endTime": "2018-07-04T02:00:00+0000"}
]

指向此html的 :

代码语言:javascript
复制
<table class="table table-bordered table-hover table-condensed">
<thead><tr><th title="Field #1">kubernetes.pod.name</th>
<th title="Field #2">container.id</th>
<th title="Field #3">memory.used.percent</th>
<th title="Field #4">cpu.used.percent</th>
<th title="Field #5">memory.bytes.used (mB)</th>
<th title="Field #6">cpu.cores.used (millicores)</th>
<th title="Field #7">endTime</th>
</tr></thead>
<tbody><tr>
<td>sds-endpoints-6-hn0fe2l</td>
<td>d19e001824978</td>
<td align="right">102</td>
<td align="right">7</td>
<td align="right">2067</td>
<td align="right">9</td>
<td>2018-07-04T02:00:00+0000</td>
</tr>
<tr>
<td>product-service-endpoints-4-da1w</td>
<td>4dd6447f5e14</td>
<td align="right">84</td>
<td align="right">7</td>
<td align="right">1698</td>
<td align="right">8</td>
<td>2018-07-04T02:00:00+0000</td>
</tr>
</tbody></table>

在python中是如何实现的(相关?) :

代码语言:javascript
复制
from json2html import *

print json2html.convert(json = json_data)    

with open("jsonREPORT.html", "w") as write_file:
    json.dump(json2html.convert(json = json_data), write_file ,sort_keys=True, indent=4)

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-07 03:56:40

如果你正在读这篇文章,并且你也在想,我已经找到了一个方法。

代码语言:javascript
复制
def inputFile = new File("D:\\Github\\rest-api testing\\hm\\out.json")
def InputJSON = new JsonSlurper().parseText(inputFile.text)


def writer = new StringWriter()  // html is written here by markup builder
def markup = new groovy.xml.MarkupBuilder(writer)  // the builder

// MAKE OF HTML
markup.html{
markup.table(class:"table table-bordered table-hover table-condensed") {
markup.thead{
markup.tr {
    markup.th(title:"Field #1", "kubernetes.pod.name")
    markup.th(title:"Field #2", "container.id")
    markup.th(title:"Field #3", "memory.used.percent")
    markup.th(title:"Field #4", "cpu.used.percent")
    markup.th(title:"Field #5", "memory.bytes.used (mB)")
    markup.th(title:"Field #6", "cpu.cores.used (millicores)")
    markup.th(title:"Field #7", "endTime")
} // tr
} // thead
markup.tbody{
markup.tr{ for (def data : InputJSON.data) {
    markup.tr{
        markup.td(align:"right",data.d[0])
        markup.td(align:"right",data.d[1])
        markup.td(align:"right",Math.round((data.d[2]) * 100))
        markup.td(align:"right",Math.round((data.d[3]) * 100))
        markup.td(align:"right",Math.round((data.d[4]) * 0.000001))
        markup.td(align:"right",Math.round(((data.d[5]) * 1000)))
        markup.td(align:"right",new Date(((long) InputJSON.end) * 1000))
    } // foreach
} // td
} // tr
} //tbody
} // table
}

println writer.toString()
票数 3
EN

Stack Overflow用户

发布于 2018-07-07 03:23:35

test.json

代码语言:javascript
复制
[
    {"f1":12345, "f2":"abcdfg"},
    {"f1":67890, "f2":"qwerty"}
]

test.gsp

代码语言:javascript
复制
<table>
    <tr>
        <td>f1</td>
        <td>f2</td>
    </tr>
    <% for(r in data) { %>
    <tr>
        <td><%= r.f1 %></td>
        <td><%= r.f2 %></td>
    </tr>
    <% } %>
</table>

groovy代码:

代码语言:javascript
复制
import groovy.json.JsonSlurper
import groovy.text.SimpleTemplateEngine

new File("/test.html").withWriter("UTF-8"){writer->
    new SimpleTemplateEngine()
        .createTemplate( new File("/test.gsp") )
        .make( data:new JsonSlurper().parse(new File("/test.json")) )
        .writeTo( writer )
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51213662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档