Alo*_*ius 127 javascript json
我试图循环通过以下json数组:
{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
}, {
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}
Run Code Online (Sandbox Code Playgroud)
并尝试了以下
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(data[key].id);
}
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我只得到第一部分,id 1值.
有任何想法吗?
Nik*_*las 196
您的JSON应如下所示:
var json = [{
"id" : "1",
"msg" : "hi",
"tid" : "2013-05-05 23:35",
"fromWho": "hello1@email.se"
},
{
"id" : "2",
"msg" : "there",
"tid" : "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}];
Run Code Online (Sandbox Code Playgroud)
您可以像这样遍历数组:
for(var i = 0; i < json.length; i++) {
var obj = json[i];
console.log(obj.id);
}
Run Code Online (Sandbox Code Playgroud)
或者像这样(从Eric建议)小心IE支持
json.forEach(function(obj) { console.log(obj.id); });
Run Code Online (Sandbox Code Playgroud)
The*_*ght 25
您的代码中存在一些问题,首先您的json必须如下所示:
var json = [{
"id" : "1",
"msg" : "hi",
"tid" : "2013-05-05 23:35",
"fromWho": "hello1@email.se"
},
{
"id" : "2",
"msg" : "there",
"tid" : "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}];
Run Code Online (Sandbox Code Playgroud)
接下来,您可以像这样迭代:
for (var key in json) {
if (json.hasOwnProperty(key)) {
alert(json[key].id);
alert(json[key].msg);
}
}
Run Code Online (Sandbox Code Playgroud)
它给出了完美的结果.
请看这里的小提琴:http://jsfiddle.net/zrSmp/
小智 13
var arr = [
{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
}, {
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}
];
Run Code Online (Sandbox Code Playgroud)
forEach方法,易于实现.
arr.forEach(function(item){
console.log('ID: ' + item.id);
console.log('MSG: ' + item.msg);
console.log('TID: ' + item.tid);
console.log('FROMWHO: ' + item.fromWho);
});
Run Code Online (Sandbox Code Playgroud)
自从我开始研究它:
var data = [{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
}, {
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}]
Run Code Online (Sandbox Code Playgroud)
而这个功能
var iterateData =function(data){ for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(data[key].id);
}
}};
Run Code Online (Sandbox Code Playgroud)
你可以这样称呼它
iterateData(data); // write 1 and 2 to the console
Run Code Online (Sandbox Code Playgroud)
正如埃里克所指出的,数组的for in循环会产生意想不到的结果.引用的问题有关于利弊的冗长讨论.
但似乎以下内容相当节省:
for(var i = 0; i < array.length; i += 1)
Run Code Online (Sandbox Code Playgroud)
虽然铬测试有以下结果
var ar = [];
ar[0] = "a";
ar[1] = "b";
ar[4] = "c";
function forInArray(ar){
for(var i = 0; i < ar.length; i += 1)
console.log(ar[i]);
}
// calling the function
// returns a,b, undefined, undefined, c, undefined
forInArray(ar);
Run Code Online (Sandbox Code Playgroud)
.forEach()至少在chrome 30中,这可以按预期工作
var logAr = function(element, index, array) {
console.log("a[" + index + "] = " + element);
}
ar.forEach(logAr); // returns a[0] = a, a[1] = b, a[4] = c
Run Code Online (Sandbox Code Playgroud)
for inmdnfor in不那么糟糕这是工作。我刚刚在JSON数据中添加了方括号。数据为:
var data = [
{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
},
{
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}
]
Run Code Online (Sandbox Code Playgroud)
循环是:
for (var key in data) {
if (data.hasOwnProperty(key)) {
alert(data[key].id);
}
}
Run Code Online (Sandbox Code Playgroud)
如果要迭代它,它必须是一个数组.你很可能缺少[和].
var x = [{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
}, {
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}];
var $output = $('#output');
for(var i = 0; i < x.length; i++) {
console.log(x[i].id);
}
Run Code Online (Sandbox Code Playgroud)
看看这个jsfiddle:http://jsfiddle.net/lpiepiora/kN7yZ/
小智 6
你的数据片段需要扩展一点,它必须是正确的 json。注意我只包含数组名称属性“item”
{"item":[
{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
}, {
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}]}
Run Code Online (Sandbox Code Playgroud)
你的java脚本很简单
var objCount = json.item.length;
for ( var x=0; x < objCount ; xx++ ) {
var curitem = json.item[x];
}
Run Code Online (Sandbox Code Playgroud)
好吧,我只能看到有两个 JSON 对象,用逗号分隔。如果它们都在数组 ( [...]) 内,那就更有意义了。
而且,如果它们位于数组内部,那么您只需使用标准的“for var i = 0...”类型的循环。事实上,我认为它将尝试检索字符串“1”的“id”属性,然后检索“hi”的“id”属性,依此类推。
有点晚了,但我希望我可以帮助别人:D
你的json需要看起来像Niklas已经说过的话.然后你走了:
for(var key in currentObject){
if(currentObject.hasOwnProperty(key)) {
console.info(key + ': ' + currentObject[key]);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你有一个多维数组,这是你的代码:
for (var i = 0; i < multiDimensionalArray.length; i++) {
var currentObject = multiDimensionalArray[i]
for(var key in currentObject){
if(currentObject.hasOwnProperty(key)) {
console.info(key + ': ' + currentObject[key]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这个
var json = [{
"id" : "1",
"msg" : "hi",
"tid" : "2013-05-05 23:35",
"fromWho": "hello1@email.se"
},
{
"id" : "2",
"msg" : "there",
"tid" : "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}];
json.forEach((item) => {
console.log('ID: ' + item.id);
console.log('MSG: ' + item.msg);
console.log('TID: ' + item.tid);
console.log('FROMWHO: ' + item.fromWho);
});
Run Code Online (Sandbox Code Playgroud)
非常简单的方法!
var tire_price_data = JSON.parse('[{"qty":"250.0000","price":"0.390000"},{"qty":"500.0000","price":"0.340000"},{"qty":"1000.0000","price":"0.290000"}]');
tire_price_data.forEach(function(obj){
console.log(obj);
console.log(obj.qty);
console.log(obj.price);
})
Run Code Online (Sandbox Code Playgroud)
谢谢。