问题描述
嗨Ppl,
我试图将Json ouptut循环到for循环并将它们存储到另一个数组中,因为我只需要来自Json的特定值... 。
这里是代码...我无法获取DTSTART,DTEND字段..我猜它是因为命名..我无法改变param名字,因为我从输出中收到它。
任何想法如何获得这些id的值?谢谢
Hi Ppl,
I am trying to loop the Json ouptut into for loop and store them into another array as i need only specific values from Json....
here is below code... i am unable to fetch DTSTART,DTEND fields.. i guess its because of naming.. i wont be able to change the param names as i receive it from output.
Any idea how to get the value of those ids also? Thanks
<!DOCTYPE html>
<html>
<body>
<p>Looping through arrays inside arrays.</p>
<p id="demo"></p>
<script>
var myObj, i, j, x = "";
myObj = {
"cars": [
{
"DTSTART;TZID=America/New_York": "20170211T123000",
"DTEND;TZID=America/New_York": "20170211T133000",
"RRULE": "FREQ=WEEKLY;BYDAY=SA",
"DTSTAMP": "20170213T195454Z",
"UID": "[email protected]",
"CREATED": "20170211T152911Z",
"DESCRIPTION": "",
"LASTMODIFIED": "20170211T152911Z",
"LOCATION": "",
"SEQUENCE": "0",
"STATUS": "CONFIRMED",
"SUMMARY": "monday meeting",
"TRANSP": "OPAQUE"
}
]
}
for (i in myObj.cars) {
x += "<h1>" + myObj.cars[i].SUMMARY + "</h1>";
x += "<h1>" + myObj.cars[i].DTSTART + "</h1>";
}
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
我尝试过:
以下是执行代码
[]
推荐答案
for (var i = 0; i < myObj.cars.length; i++){
var obj = myObj.cars[i];
for (var key in obj){
if (key == "SUMMARY") {
x += "<h1>" + obj[key] + "</h1>";
}
if (key.startsWith("DTSTART")) {
x += "<h2>" + obj[key] + "</h2>";
}
}
}
输出:
20170211T123000
星期一会议
Output:
20170211T123000
monday meeting
myObj.cars[i]["DTEND;TZID=America/New_York"]
尽管如此,尽量避免像这样的异常密钥,因为它会打破那些通常的JSON处理代码。
Nevertheless, try to avoid unusual keys like this as it is going to break those usual JSON handling code.
这篇关于将json数据循环到for循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!