javascript - How to access JSON element from jQuery Ajax -


i want target specific element in json file:

{   "taskmeta": "some meta info",   "taskslib": [     {      "task001":          {             "id":"1",             "createdate":"01.02.17",             "duedate":"02.03.17",             "author":"author name",             "tag":"things",             "description":"here's description of todo",             "priority":"1",             "color":"danger",             "title":"btn-danger",             "content":"here's notes content"          },      "task002":          {             "id":"2",             "createdate":"02.02.17",             "duedate":"05.03.17",             "author":"author name",             "tag":"other things",             "description":"here's description of todo",             "priority":"0",             "color":"info",             "title":"foo",             "content":"here's amazing content"          }       }   ] } 

then gets loaded in js file:

$.ajax({      type:     'get',      url:      'includes/tasks.json',      datatype: 'json',      success: function(task) {          $.each(task, function(i, task){            console.log(               task.tasklib[0].id            ); ... 

this gives me:

uncaught typeerror: cannot read property '0' of undefined

you have first parse json:

var tasksdata = json.parse(task); 

then can loop through tasks below:

$.each(tasksdata.taskslib, function(i, task){     console.log(task.id); } 

Comments