2

I have a string like this one:

{
    "products":{"56":"productName","28":"productName"},
    "excludedProducts":{"83":"productName","1":"poductName"}
}

So what I want is to get an object in javascript which looks like this:

{
     products: {
         "56": "productName",
         "28": "productName"
     },
     excludedProducts: {
         "83": "productName",
         "1": "productName"
     }
}

But JSON.parse() converts numbers into indexes and I get

{
     products: {
         28: "productName",
         56: "productName"
     },
     excludedProducts: {
         83: "productName",
         1: "productName"
     }
}

So basically, is there a way to preserve order of elements after parsing the string formatted like that?

wazelin
  • 661
  • 11
  • 20

1 Answers1

3

You need to make use of Arrays to preserve the formatting.

Shankar Narayana Damodaran
  • 66,874
  • 43
  • 94
  • 124
  • Right. My bad. I ended up using JSON formatted like this: {"products":[[25,"productName"],...],"excludedProducts":[[83,"productName"],...]} – wazelin Aug 14 '13 at 05:00