0

I am using the jQuery plugin to convert the XML content to JSON, but I found that the ordering of JSON content is not correct:

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>

The converted JSON object:

{
  "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"
      }
    ]
  }
}

According to the XML, it is itemb, item, item, item, item, item, itemhl, item, but the JSON is itemb, item, item, item, item, item, item, itemhl. Could someone please suggest how to fix it, thanks.

Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
  • Is there some specific reason you want it as an object rather than an xml document? – Kevin B Feb 27 '13 at 15:40
  • *"Could someone please suggest how to fix it, thanks."* how would you expect it to be handled instead? – Kevin B Feb 27 '13 at 15:47
  • JSON key's don't have to be ordered in any particular way. See similar question http://stackoverflow.com/questions/3948206/json-order-mixed-up – Danny Feb 27 '13 at 15:50

1 Answers1

0

You can't have 2 key-value pairs with the same key in JSON.

I believe what you want is the following, but that is not technically possible to my knowledge.

{
    "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"
  }
Brad M
  • 7,857
  • 1
  • 23
  • 40