0

I am working on SpringMVC. In my controller I create a JSON object and I pass that object to JavaScript. How can I read that object in JavaScript?

My Map object

Map<String rootNode,List<String>> map = new HashMap<String rootNode,List<String>();
String rootNode = "bhanu";
ArrayList<String> al = new ArrayList<String>();
for( int i = 0; i < UserProfile.size; i++ ) { 
    al.add( userProfile.get( i ) );
}
map.put( userProfile, al );

At last my Map object has this data:

{
   "Bhanu":["hari@gmail.com","balu@gmail.com","santha@gmail.com"],
   "root":["bhanu@gmail.com","anu@gmail.com","apar@gmail.com"],
   "hari":["bhanuprasad@gmail.com","sravya@gmail.com","mahesh@gmail.com"],
   "balu":["rama@gmail.com"]
}

Now I convert this object with GSon:

Gson gson = new GSon();
String orgChartUsers = gson.toJson(map);

Now I passed this string to JavaScript because this is my Ajax response:

function orgUsers( result ) {
    var orgObject = JSON.parse( result );  

         for(var name in result) {
                console.log(name + "=" + result[name]);


               }
}

This is working fine but i want to fetch data like this first i want to fetch data from "root" in root i have some data when i read root for example i got bhanu@gmail.com now i want to fetch the data from Bhanu here i got some data like hari@gmail.com again i want to fetch data for hari like this i want how can i do this any one help me

user2767541
  • 97
  • 2
  • 3
  • 11
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Sep 11 '13 at 06:56
  • I also recommend to read a JavaScript tutorial to learn the basics about arrays and objects: http://eloquentjavascript.net/chapter4.html. – Felix Kling Sep 11 '13 at 07:00

3 Answers3

1

Your lists will become javascript arrays, so for example you could use:

window.alert (orgObject["Bhanu"][0]);

which should pop up "hari@gmail.com" given your example data.

See How to list the properties of a JavaScript object for details of how to list the keys of your map, should you need this.

Community
  • 1
  • 1
Jules
  • 14,841
  • 9
  • 83
  • 130
  • Hi Thanks for the replay.It's working fine but i have one doubt ..by using this output i want to generate orgChart .means for example i want to read orgObject["root"] under root i got some users "root":["bhanu@gmail.com","anu@gmail.com","apar@gmail.com"] now when i read firstnode bhanu@gmail.com i want to check any json object is there with bhanu if it is there i want to fetch those users how can i do this – user2767541 Sep 11 '13 at 07:31
0

The result will be either an object or array.

To iterate over objects you can use the for-in loop

for (var key in obj) {
    if(obj.hasOwnProperty(key)) {
        // do something here
    }
}

To iterate over an array you can use a normal loop:

for (var i = 0, ilen = array.length; i < ilen; i += 1) {
    // do something here
}
Emil A.
  • 3,387
  • 4
  • 29
  • 46
0

Here is the most simple way

var result = {"Bhanu":["hari@gmail.com","balu@gmail.com","santha@gmail.com"],"root":["bhanu@gmail.com","anu@gmail.com","apar@gmail.com"],"hari":["bhanuprasad@gmail.com","sravya@gmail.com","mahesh@gmail.com"],"balu":["rama@gmail.com"]};

for(var name in result) {
    console.log(name + "=" + result[name]);
}

This outputs:

Bhanu=hari@gmail.com,balu@gmail.com,santha@gmail.com
root=bhanu@gmail.com,anu@gmail.com,apar@gmail.com
hari=bhanuprasad@gmail.com,sravya@gmail.com,mahesh@gmail.com
balu=rama@gmail.com

Have in mind that the arrays are actually cast to a string. So result[name] is actually an array.

Krasimir
  • 13,306
  • 3
  • 40
  • 55
  • Hi Thanks for the replay small doubt ..i want to read first what is the data in "root" then remaning data how can i do this.. – user2767541 Sep 11 '13 at 06:59
  • You may use *.length* property to find out is it an array or object. For example *result.length*. If it returns something then it is an array. – Krasimir Sep 11 '13 at 07:16