将XML转换为JSON,但内容与jQuery插件不一致

时间:2013-02-27 15:32:20

标签: jquery xml json

我使用jQuery plugin将XML内容转换为JSON,但我发现JSON内容的顺序不正确:

XML:

<list>
    <row>
        <item>interest</item>
        <item>7</item>
        <item>10</item>
        <item>13</item>
        <item>15</item>
        <item>20</item>
        <item>25</item>
        <item>30</item>
    </row>
    <row>
        <item>1.000</item>
        <item>$8,631.87</item>
        <item>$6,132.29</item>
        <item>$4,787.03</item>
        <item>$4,189.46</item>
        <item>$3,219.26</item>
        <item>$2,638.11</item>
        <item>$2,251.48</item>
    </row>
    <row>
        <item>1.630</item>
        <item>$8,823.44</item>
        <item>$6,325.61</item>
        <item>$4,982.41</item>
        <item>$4,386.29</item>
        <item>$3,419.83</item>
        <item>$2,842.51</item>
        <item>$2,459.75</item>
    </row>
    <row>
        <itemb>2.150</itemb>
        <item>$8,983.59</item>
        <item>$6,488.07</item>
        <item>$5,147.43</item>
        <item>$4,553.07</item>
        <item>$3,591.13</item>
        <itemhl>$3,018.36</itemhl>
        <item>$2,640.16</item>
    </row>
</list>

转换后的JSON对象:

{
  "list": {
    "row": [
      {
        "item": [
          "interest",
          "7",
          "10",
          "13",
          "15",
          "20",
          "25",
          "30"
        ]
      },
      {
        "item": [
          "1.000",
          "$8,631.87",
          "$6,132.29",
          "$4,787.03",
          "$4,189.46",
          "$3,219.26",
          "$2,638.11",
          "$2,251.48"
        ]
      },
      {
        "item": [
          "1.630",
          "$8,823.44",
          "$6,325.61",
          "$4,982.41",
          "$4,386.29",
          "$3,419.83",
          "$2,842.51",
          "$2,459.75"
        ]
      },
      {
        "itemb": "2.150",
        "item": [
          "$8,983.59",
          "$6,488.07",
          "$5,147.43",
          "$4,553.07",
          "$3,591.13",
          "$2,640.16"
        ],
        "itemhl": "$3,018.36"
      }
    ]
  }
}

根据XML,它是itembitemitemitemitemitem,{{1} },itemhl,但JSON是itemitembitemitemitemitem,{{ 1}},item。有人可以建议如何解决它,谢谢。

1 个答案:

答案 0 :(得分:0)

在JSON中,您不能拥有2个具有相同键的键值对。

我相信你想要的是以下内容,但据我所知,这在技术上是不可能的。

{
    "itemb": "2.150",
    "item": [
      "$8,983.59",
      "$6,488.07",
      "$5,147.43",
      "$4,553.07",
      "$3,591.13"
    ],
    "itemhl": "$3,018.36",
    "item": "$2,640.16"
  }