我正在尝试转换
jsonOutput ="{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}";
使用Groovy到XML格式?
答案 0 :(得分:0)
json字符串无效,但假设它已修复,那么你可以这样做:
def jsonOutput = '{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}]}}'
def parsed = new groovy.json.JsonSlurper().parseText(jsonOutput)
def sw = new StringWriter()
new groovy.xml.MarkupBuilder(sw).with {
doubleQuotes = true
visitor = { k, v ->
"$k" { v instanceof Map ? v.collect(visitor) : v instanceof Collection ? v.eachWithIndex { item,i -> "$i"(item.id, item) } : mkp.yield(v) }
}
data(type: 'current') { parsed.collect visitor }
}
println sw.toString()
将产生以下结果:
<data type="current">
<id>108013515952807</id>
<posts>
<data>
<0 id="108013515952807_470186843068804" created_time="2013-05-14T20:43:28+0000">108013515952807_470186843068804</0>
<1 id="108013515952807_470178529736302" message="TEKST" created_time="2013-05-14T20:22:07+0000">108013515952807_470178529736302</1>
</data>
</posts>
</data>
希望这会给你一些工作......