JSON是否有等价的XSLT ?它允许我对JSON进行转换,就像XSLT对XML所做的那样。


当前回答

很久以前,我为我的基于jackson的json处理框架写了一个dom适配器。它用了。xom图书馆。生成的dom树可以使用java xpath和xslt工具。我做了一些非常简单的实现选择。例如,根节点总是被称为“root”,数组进入一个带有li子元素的ol节点(就像在html中一样),而其他一切都只是一个带有原始值或另一个对象节点的子节点。

JsonXmlConverter.java

用法: JsonObject sampleJson = sampleJson(); org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, “root”);

其他回答

我使用骆驼路由marshal(xmljson) ->到(xlst) -> marshal(xmljson)。足够高效(虽然不是100%完美),但简单,如果你已经在使用Camel。

作为一个老问题的另一个新答案,我建议看看defantjs。它不是JSON的XSLT等价物,它是JSON的XSLT。文档的“模板”部分包括以下示例:

<!-- Defiant template -->
<script type="defiant/xsl-template">
    <xsl:template name="books_template">
        <xsl:for-each select="//movie">
            <xsl:value-of select="title"/><br/>
        </xsl:for-each>
    </xsl:template>
</script>

<script type="text/javascript">

var data = {
        "movie": [
            {"title": "The Usual Suspects"},
            {"title": "Pulp Fiction"},
            {"title": "Independence Day"}
        ]
    },
    htm = Defiant.render('books_template', data);

console.log(htm);
// The Usual Suspects<br>
// Pulp Fiction<br>
// Independence Day<br>

I've been really tired of the enormous amount of JavaScript templating engines out there, and all their inline HTML-templates, different markup styles, etc., and decided to build a small library that enables XSLT formatting for JSON data structures. Not rocket science in any way -- it's just JSON parsed to XML and then formatted with a XSLT document. It's fast too, not as fast as JavaScript template engines in Chrome, but in most other browsers it's at least as fast as the JS engine alternative for larger data structures.

jq——轻量级灵活的命令行JSON处理器

它不像XSLT那样基于模板,但更简洁。例如,将名称和地址字段提取到数组中:[.name, .address]

本教程介绍了一个转换Twitter JSON API的示例(手册中有很多示例)。

为什么不使用Mr. Data Coverter将JSON转换为XML,然后使用XSLT将其转换为JSON呢?