• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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

 
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...............
 
Author
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 ?
 
Marshal
Posts: 67388
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic