作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想定位我的 JSON 文件中的特定元素:
{
"taskMeta": "Some meta info",
"tasksLib": [
{
"task001":
{
"id":"1",
"createDate":"01.02.17",
"dueDate":"02.03.17",
"author":"Author name",
"tag":"Things",
"description":"Here's a description of the todo",
"priority":"1",
"color":"danger",
"title":"btn-danger",
"content":"Here's the notes content"
},
"task002":
{
"id":"2",
"createDate":"02.02.17",
"dueDate":"05.03.17",
"author":"Author name",
"tag":"Other things",
"description":"Here's another description of the todo",
"priority":"0",
"color":"info",
"title":"Foo",
"content":"Here's some amazing content"
}
}
]
}
然后它被加载到这个 js 文件中:
$.ajax({
type: 'GET',
url: 'includes/tasks.json',
dataType: 'json',
success: function(task) {
$.each(task, function(i, task){
console.log(
task.taskLib[0].id
);
...
这给了我:
Uncaught TypeError: Cannot read property '0' of undefined
最佳答案
您必须首先解析您的 JSON:
var tasksData = JSON.parse(task);
然后您可以按如下方式循环执行您的任务:
$.each(tasksData.tasksLib, function(i, task){
console.log(task.id);
}
关于javascript - 如何从 jQuery Ajax 访问 JSON 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238636/
我是一名优秀的程序员,十分优秀!