This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print java Map name/value pair through JSON object in JS

 
Kuts Aryan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am using struts2 framework and from my js file I am invoking my action class through AJAX call.In the success part of ajax I am getting a JSON object of java map like below


where typeModel is a object of DeviceTypeModel class and DeviceTypeModel class is having a java Map <String,String> class variable.
My question is in the java script part after getting the json object, how can I access this map key/value pair using JSON so that in the jsp page I can print the name/value pair
for the whole map object.

Please help me...............
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking how to create DOM elements for each key/value pair in the JavaScript object? Which part of that specifically given you a problem, iterating over an object's properties, or creating the DOM elements?

Please don't post the Struts-specific part in the HTML/JS forum--it's completely irrelevant to what you're trying to do. How the JSON is retrieved is totally irrelevant--you're basically asking how to iterate this:Right?
 
Kuts Aryan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply and you are correct.I want to know about the iteration of JSON object.Right now I am doing the iteration like below


It is working but the order of displaying the element is not proper.If my JSON object contains

var foo = { "A":"a","B":"b","C":"c","D":"d"};
It is sometimes printing the oder like
D:d
A:a
B:b
C:c
like this.......Is there any way to display the order properly ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object properties are not ordered. So there is no such thing as "proper order".

If you want ordered, use an array.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic