使用jQuery将JSON转换为XML

时间:2021-04-24 21:46:55

I need to convert JSON to XML (using jquery), but in this way:

我需要将JSON转换为XML(使用jquery),但是这样:

If I have JSON looking like this:

如果我有这样的JSON:

{
name: "sampleConfiguration",
description: "Configuration used for demonstration purposes"
version: "1.0",
parameters: [
    {
        name: "sampleParameter",
        description: "parameter description",
        value: "20",
        enabled: "1"
    },
    {
        name: "items",
        description: "parameter with subparameters",
        value:[
            {
                name: "item",
                description: "nested parameter",
                value: "13"
            },
            {
                name: "item",
                description: "nested parameter 2",
                value: "TEST"
            }
        ]
    }
]}

Then I should convert it to the XML looking like this:

然后我应该将它转换为如下所示的XML:

<?xml version="1.0"?>
<sampleConfiguration version="1.0" description="Configuration used for demonstration purposes">
<params>
    <sampleParameter enabled="1" description="parameter description">20</sampleParameter>
    <items description="parameter with subparameters">
        <item description="nested parameter">13</item>
        <item description="nested parameter 2">TEST</item>
    </items>
</params>
</sampleConfiguration>

I have been looking for JSON to XML converting code, but I always get XML looking like this (which is what I don't need):

我一直在寻找JSON到XML转换代码,但我总是得到这样的XML(这是我不需要的):

<?xml version="1.0"?>
<name>sampleConfiguration</name>
<description>Configuration used for demonstration purposes</description>
<version>1.0</version>
<parameters>
   <name>sampleParameter</name>
   <description>parameter description</description>
   <value>20</value>
   <enabled>1</enabled>
</parameters>
<parameters>
   <name>items</name>
   <description>parameter with subparameters</description>
   <value>
      <name>item</name>
      <description>nested parameter</description>
      <value>13</value>
   </value>
   <value>
      <name>item</name>
      <description>nested parameter 2</description>
      <value>TEST</value>
   </value>
</parameters>

1 个解决方案

#1


0  

I think you are looking for this:-

我想你正在寻找: -

http://code.google.com/p/x2js/

http://code.google.com/p/x2js/

This library provides XML to JSON (JavaScript Objects) and vice versa javascript conversion functions. The library is very small and doesn't require any other additional libraries. :)

该库提供XML到JSON(JavaScript对象),反之亦然javascript转换函数。该库非常小,不需要任何其他库。 :)

#1


0  

I think you are looking for this:-

我想你正在寻找: -

http://code.google.com/p/x2js/

http://code.google.com/p/x2js/

This library provides XML to JSON (JavaScript Objects) and vice versa javascript conversion functions. The library is very small and doesn't require any other additional libraries. :)

该库提供XML到JSON(JavaScript对象),反之亦然javascript转换函数。该库非常小,不需要任何其他库。 :)