簡體   English   中英

如何使用Groovy腳本將json轉換為html

[英]How to convert a json to a html using a Groovy script

顧名思義,我正在嘗試使用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中如何?

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 )
}

如果您閱讀此書,而您也想知道相同的內容,我已經找到了一種方法。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM