1

I'm parsing my string value to json object. while doing this it returns the json object with different order.

var l = '{"creationType":"ITEMCLASSES","maxLevelNo":"4","selectbox1":"11001","textbox11":"KRAFT PAPER","hidden11":"11001","textbox12":"FG2","hidden12":"11051","textbox13":"HMC2","hidden13":"11045","textbox14":"2","textbox15":"LS2","hidden15":"11048","textbox16":"123","hidden16":"11015","selectbox2":"11002","textbox21":"kraft ","hidden21":"11057","selectbox3":"11003","textbox31":"40 BF","hidden31":"11004","textbox32":"Natural Color","hidden32":"11006","textbox33":"2","textbox41":"140 GSM","hidden41":"11008"}';       
var o = JSON.parse(l);
alert(0);

resultant object is ordered based on the name. how to avoid this situation? kindly give your suggestion.

Cœur
  • 37,241
  • 25
  • 195
  • 267
PravinKumar.R
  • 335
  • 1
  • 3
  • 15
  • possible duplicate of [Elements order in a "for (… in …)" loop](http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop) – Travis J Jan 18 '14 at 09:40
  • Object order is not guaranteed. There is more information in the duplicate. – Travis J Jan 18 '14 at 09:40

2 Answers2

0

Your variable "1" is already a JSON object. This means that feeding it into the JSON.parse function is both redundant and unnescesarry. The JSON.parse(); function is primarily used when converting strings to JSON objects.

var l = {"creationType":"ITEMCLASSES","maxLevelNo":"4","selectbox1":"11001","textbox11":"KRAFT PAPER","hidden11":"11001","textbox12":"FG2","hidden12":"11051","textbox13":"HMC2","hidden13":"11045","textbox14":"2","textbox15":"LS2","hidden15":"11048","textbox16":"123","hidden16":"11015","selectbox2":"11002","textbox21":"kraft ","hidden21":"11057","selectbox3":"11003","textbox31":"40 BF","hidden31":"11004","textbox32":"Natural Color","hidden32":"11006","textbox33":"2","textbox41":"140 GSM","hidden41":"11008"};
alert(1);

Just skip the second line and manipulate your "1" variable directly.

VCNinc
  • 747
  • 1
  • 9
  • 25
0

You can't.

As described by www.json.org

In JSON, they take on these forms:

An object is an unordered set of name/value pairs

Use an array if you want to maintain the order.

thomas.g
  • 3,894
  • 3
  • 29
  • 36