任何人都可以給我一個正確的解決方案,這個program.how使用foreach循環打印所有的值。我試過,但我得到一個錯誤,因爲未定義的值預先感謝...我在等待答覆...在nodejs中使用循環訪問json元素
differ.json:
{
"table":
[
{
"place":
{
"STATE":"TAMILNADU",
"DISTRICT":"VELLORE"
}
},
{
"place":
{
"STATE":"KERALA",
"DISTRICT":"ALAPHUZHA"
}
},
{
"place":
{
"STATE":"ANDHRA PRAADESH",
"DISTRICT":"KUNTUR"
}
}
]
}
Nodejs file:
var fs = require('fs');
var obj1 = fs.readFileSync('differ.json', 'utf8');
var data=JSON.parse(obj1);
var countobjects = Object.keys(data).length;
console.log(countobjects);
for(var place in data.table)
{
for(var field in data.table[place])
{
console.log("State:" + data.table[place].field);
console.log("District:" + data.table[place].field);
}
}
爲您快速的回覆非常感謝兄弟是工作完美... – vigneshRavi