Have a problem JSON and Hash.

Hi everybody.

I have using ActiveSupport::JSON library in rails

I have a json string: test= {"C1":{"id":"C1","mode":"new","type":"block","name":"new block C1","unit":"","choices":,"index":10},"R1":{"id":"R1","mode":"new","type":"text","name":"new question R1","unit":"","choices":[""],"index":20},"R2":{"id":"R2","mode":"new","type":"text","name":"new question R2","unit":"","choices":[""],"index":30},"R3":{"id":"R3","mode":"new","type":"text","name":"new question R3","unit":"","choices":[""],"index":40},"C2":{"id":"C2","mode":"new","type":"block","name":"new block C2","unit":"","choices":,"index":50},"R4":{"id":"R4","mode":"new","type":"text","name":"new question R4","unit":"","choices":[""],"index":60},"C3":{"id":"C3","mode":"new","type":"block","name":"new block C3","unit":"","choices":,"index":70}}

when i using:

temp = ActiveSupport::JSON result = temp.decode(test)

and I recieve: Result is not sort the same as param "test" i had before.

result ="R2choicesnamenew question R2modenewunittypetextidR2index30R3choicesnamenew question R3modenewunittypetextidR3index40R4choicesnamenew question R4modenewunittypetextidR4index60C1choicesnamenew block C1modenewunittypeblockidC1index10C2choicesnamenew block C2modenewunittypeblockidC2index50C3choicesnamenew block C3modenewunittypeblockidC3index70R1choicesnamenew question R1modenewunittypetextidR1index20"

I dont understand it. Please tell me a solution. Thanks for help.

Hi everybody.

I have using ActiveSupport::JSON library in rails

I have a json string: test= {"C1":{"id":"C1","mode":"new","type":"block","name":"new block C1","unit":"","choices":,"index":10},"R1":{"id":"R1","mode":"new","type": "text","name":"new question R1","unit":"","choices":[""],"index":20},"R2":{"id":"R2","mode":"new","type ":"text","name":"new question R2","unit":"","choices":[""],"index":30},"R3":{"id":"R3","mode":"new","type ":"text","name":"new question R3","unit":"","choices":[""],"index":40},"C2":{"id":"C2","mode":"new","type ":"block","name":"new block C2","unit":"","choices":,"index":50},"R4":{"id":"R4","mode":"new","type": "text","name":"new question R4","unit":"","choices":[""],"index":60},"C3":{"id":"C3","mode":"new","type ":"block","name":"new block C3","unit":"","choices":,"index":70}}

That is not a string, but trying to assign a new hash to test. I'll assume that the you just posted test= to define your problem.

when i using:

temp = ActiveSupport::JSON result = temp.decode(test)

The call to decode will create a hash From ruby-doc.org:

"A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order."

result = result.sort

<%= result.inspect %>

Seems to sort your hash by the keys. If input order is what you are looking for, not sure how to help without modifying your keys (prepending an index).

Steve Alex