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":["[email protected]","[email protected]","[email protected]"],
"root":["[email protected]","[email protected]","[email protected]"],
"hari":["[email protected]","[email protected]","[email protected]"],
"balu":["[email protected]"]
}
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 [email protected] now i want to fetch the data from Bhanu here i got some data like [email protected] again i want to fetch data for hari like this i want how can i do this any one help me
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":["[email protected]","[email protected]","[email protected]"],
"root":["[email protected]","[email protected]","[email protected]"],
"hari":["[email protected]","[email protected]","[email protected]"],
"balu":["[email protected]"]
}
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 [email protected] now i want to fetch the data from Bhanu here i got some data like [email protected] again i want to fetch data for hari like this i want how can i do this any one help me
Share Improve this question edited Sep 11, 2013 at 10:05 user2767541 asked Sep 11, 2013 at 6:52 user2767541user2767541 972 gold badges3 silver badges11 bronze badges 2- possible duplicate of Access / process (nested) objects, arrays or JSON – Felix Kling Commented Sep 11, 2013 at 6:56
- I also remend to read a JavaScript tutorial to learn the basics about arrays and objects: eloquentjavascript/chapter4.html. – Felix Kling Commented Sep 11, 2013 at 7:00
3 Answers
Reset to default 1Your lists will bee javascript arrays, so for example you could use:
window.alert (orgObject["Bhanu"][0]);
which should pop up "[email protected]" 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.
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
}
Here is the most simple way
var result = {"Bhanu":["[email protected]","[email protected]","[email protected]"],"root":["[email protected]","[email protected]","[email protected]"],"hari":["[email protected]","[email protected]","[email protected]"],"balu":["[email protected]"]};
for(var name in result) {
console.log(name + "=" + result[name]);
}
This outputs:
[email protected],[email protected],[email protected]
[email protected],[email protected],[email protected]
[email protected],[email protected],[email protected]
[email protected]
Have in mind that the arrays are actually cast to a string. So result[name] is actually an array.