如何在 JavaScript 中从 JSON 对象中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18734505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fetch data from JSON object in JavaScript?
提问by user2767541
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?
我正在研究 SpringMVC。在我的控制器中,我创建了一个 JSON 对象并将该对象传递给 JavaScript。如何在 JavaScript 中读取该对象?
My Map
object
我的Map
对象
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:
最后我的Map
对象有这个数据:
{
"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 gson = new GSon();
String orgChartUsers = gson.toJson(map);
Now I passed this string to JavaScript because this is my Ajax response:
现在我将此字符串传递给 JavaScript,因为这是我的 Ajax 响应:
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 Bhanuhere i got some data like [email protected]again i want to fetch data for harilike this i want how can i do this any one help me
这工作正常,但我想首先获取这样的数据我想从root中的“root”获取数据我在读取 root 时有一些数据,例如我得到了[email protected]现在我想从Bhanu获取数据在这里,我又得到了一些像[email protected]这样的数据,我想像这样为hari获取数据,我想我该怎么做
回答by Jules
Your lists will become javascript arrays, so for example you could use:
您的列表将成为 javascript 数组,例如您可以使用:
window.alert (orgObject["Bhanu"][0]);
which should pop up "[email protected]" given your example data.
鉴于您的示例数据,它应该弹出“[email protected]”。
See How to list the properties of a JavaScript objectfor details of how to list the keys of your map, should you need this.
如果需要,请参阅如何列出 JavaScript 对象的属性以获取有关如何列出地图键的详细信息。
回答by Emil A.
The result will be either an object or array.
结果将是一个对象或数组。
To iterate over objects you can use the for-in loop
要迭代对象,您可以使用 for-in 循环
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
}
回答by Krasimir
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.
请记住,数组实际上是转换为字符串。所以result[name]实际上是一个数组。