gpt4 book ai didi

json - 转换没有根名称的 JSON 对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:05:57 24 4
gpt4 key购买 nike

我正在尝试使用 for-each 转换我的 JSON 对象,但我没有任何根元素。这是我的对象,节点数可以更多。

[       
{
"id": "1",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "2",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
},
{
"id": "3",
"href": "string",
"description": "string",
"isBundle": true,
"isCustomerVisible": true,
"name": "string",
"productSerialNumber": "string",
"productNumber": "string",
"startDate": "2018-11-27T13:26:22.783Z",
"endDate": "2018-11-27T13:26:22.783Z",
"status": "created"
}
]

我正在尝试使用我的 xsl 转换:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<jsonObject xmlns:json="http://json.org/">
<requestResponse>
<xsl:choose>
<xsl:when test="count(//id) > 0">
<returnCode>100</returnCode>
<returnMessage>SUCCESS</returnMessage>
</xsl:when>
<xsl:otherwise>
<returnCode>9999</returnCode>
<returnMessage>BUSINESS_FAULT</returnMessage>
</xsl:otherwise>
</xsl:choose>
</requestResponse>

<xsl:for-each select="@*|node()">
<xsl:if test="id">
<id>
<xsl:value-of select="/id" />
</id>
</xsl:if>
</xsl:for-each>
</jsonObject>
</xsl:template>
</xsl:stylesheet>

如果我有我的对象的根名称,我可以处理它,但我需要一些帮助。提前感谢任何想法!

最佳答案

在无根名称数组 json 中,您可以使用下面的 xslt for <id>标签。重点是<jsonArray><jsonElement></jsonElement></jsonArray>方法。但是你需要小心 <jsonElement> , 它应该在 <xsl:for-each select="//jsonArray/jsonElement"> 中堵塞。为了不暴露 for each 综合症,您不应该在其中写入字段的根元素。例如<xsl:if test="id"> .

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://www.qas.com/OnDemand-2011-03"
xmlns:json="http://json.org">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<jsonArray>
<xsl:for-each select="//jsonArray/jsonElement">
<jsonElement>
<xsl:if test="id">
<id><xsl:value-of select="id" /> </id>
</xsl:if>
</jsonElement>
</xsl:for-each>
</jsonArray>
</xsl:template>
</xsl:stylesheet>

关于json - 转换没有根名称的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53532292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com