1

我有一个以下 JSON 响应,这是我在 XSLT 转换之后得到的。数组中的值"name"需要以"ABC","XYZ"

有效载荷

<Data>
 <Mapping>
   <LocationID>001</LocationID>
   <GeoX>1.00</GeoX>
   <GeoY>2.00</GeoY>
 </Mapping>
 <Mapping>
   <LocationID>002</LocationID>
   <GeoX>56.00</GeoX>
   <GeoY>42.00</GeoY>
 <Mapping>
</Data>

实现对象的当前代码Destination

<xsl:template match="//Data">
   <Destination>
      <Locations>
          <xsl:text disable-output-escaping="yes">&lt;?xml-multiple?&gt;</xsl:text>
            <Name>
             <jsonArray>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'">"ABC"</xsl:when>
                     <xsl:when test="LocationID='002'">"XYZ"</xsl:when>
                     <xsl:otherwise>"NEW"</xsl:otherwise>
                  </xsl:choose>
                  <xsl:if test="position()!=last()">,</xsl:if>
               </xsl:for-each>
              </jsonArray>
            </Name>
        </Locations>
    </Destination>
</xsl:template>

XML 输出

<Destination>
  <Locations>
    <Name>"ABC","XYZ"</Name>
  </Locations>
</Destination>

问题 XML 到 JSON 输出

"Destination": [
    {
        "Locations": {
            "Name": [
                "\"ABC\",\"XYZ\""
            ]
        },

预期的 JSON 输出

"Destination": [
    {
        "Locations": {
            "Name": [
                "ABC","XYZ"
            ]
        },

当我将 XML 转换为 JSON 时,会出现这个 "\"ABC\",\"XYZ\"" 转义字符。有没有办法克服这个。

4

1 回答 1

1

我可以通过如下更改上述代码来解决此问题。

以前的代码:

<xsl:template match="//Data">
   <Destination>
      <Locations>
          <xsl:text disable-output-escaping="yes">&lt;?xml-multiple?&gt;</xsl:text>
            <Name>
             <jsonArray>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'">"ABC"</xsl:when>
                     <xsl:when test="LocationID='002'">"XYZ"</xsl:when>
                     <xsl:otherwise>"NEW"</xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
              </jsonArray>
            </Name>
        </Locations>
    </Destination>
</xsl:template>

更改代码:

<xsl:template match="//Data">
   <Destination>
      <Locations>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'"><Name>ABC</Name></xsl:when>
                     <xsl:when test="LocationID='002'"><Name>XYZ</Name></xsl:when>
                     <xsl:otherwise><Name>NEW</Name></xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
        </Locations>
    </Destination>
</xsl:template>

输出

"Destination": [
    {
        "Locations": {
            "Name": [
                "ABC",
                "XYZ"
            ]
        },
于 2020-06-08T16:09:37.823 回答