• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return Map via Ajax

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some unique requirement.

Through Ajax call I am populating Map in one of my controller. and I want to access that map in Javascript.

Any help on this?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not unique at all. I do it all the time.

Return the Map as the response in JSON format.

If your server code is in Java, there are any number of JSON libraries (Gson, Jackson, Stringtree, etc) that will convert a Map to its JSON equivalent with one line of code.
 
Likhit Gangavane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for Quick message,

You mean to say i need to create somthing like -
JSONObject json = new JSONObject();
json.put("res",map);

and then try to access that in my Ajax call. as i am using Spring controller, should i have to put this json abject in following format -
mav.addObject("data", json); (where mav is ModelAndView mav = new ModelAndView("jsonView");)
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea as I don't know what JSON library you are using.

You need to return the JSON as the response and interpret the JSON on the client side. (jQuery makes this really easy if you are using it.)

As you are using Spring, I'll move this to the Spring forum so that someone familiar with it can advise you on returning the JSOn as the response.
 
Likhit Gangavane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear, I was able to pass JSON as response.

i am getting below data when i tried to print-

Data{"myHashMap":"{TEMPVAR=[\\"com.print.LookUp@1dere\\"]}", TEMPVAR1=[\\"com.print.LookUp@1dere\\"], "NULL":{}}

this JSON is comprised of Key(TEMPVAR,TEMPVAR1) and corresponding array.

I am currently missing on syntax to access this JSON object on javascript end.

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using Spring 3.x then it is easy.

Just return the Map in your Controller and annotate it with @ResponseBody.

have <mvc:annotation-driven/> in your Spring config and just include the Jackson jar file. Apache Jackson is a great JSON library for Java. You just need it in your classpath for Spring to automatically convert the Map into JSON.

In your results, it looks like the map has some objects in which it seems to be just calling the default toString of that object. Jackson instead will "serialize" the data in the object's properties into JSON.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic