正如标题所示,我正在尝试使用Groovy将json转换为html。我知道如何在python中执行此操作,但是由于此代码在jenkins中运行,因此我需要使用Groovy找到相同的方法。
此json:
[
{
"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:
<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(相关?):
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)
任何建议在Groovy中如何使用?
答案 0 :(得分:3)
test.json
[
{"f1":12345, "f2":"abcdfg"},
{"f1":67890, "f2":"qwerty"}
]
test.gsp
<table>
<tr>
<td>f1</td>
<td>f2</td>
</tr>
<% for(r in data) { %>
<tr>
<td><%= r.f1 %></td>
<td><%= r.f2 %></td>
</tr>
<% } %>
</table>
常规代码:
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 )
}
答案 1 :(得分:0)
如果您阅读了此书,并且您也想知道相同的内容,我已经找到了一种方法。
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()