File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How to print java Map name/value pair through JSON object in JS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to print java Map name/value pair through JSON object in JS" Watch "How to print java Map name/value pair through JSON object in JS" New topic
Author

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

Kuts Aryan
Greenhorn

Joined: Jun 27, 2009
Posts: 14
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 27, 2009
Posts: 14
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

Joined: Jan 10, 2002
Posts: 61612
    
  67

Object properties are not ordered. So there is no such thing as "proper order".

If you want ordered, use an array.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to print java Map name/value pair through JSON object in JS