最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't Read JSON - Cannot read property of undefined - Stack Overflow

programmeradmin0浏览0评论

I've been struggling with this issue for quite a while now. When I try to console.log a JSON proprty, the following error shows up:

TypeError: Cannot read property 'timelineData' of undefined

This is what my JSON file looks like:

{
  "default": {
    "timelineData": [{
      "time": "1359676800",
      "formattedTime": "Feb 1, 2013",
      "formattedAxisTime": "Feb 1, 2013",
      "value": [51],
      "hasData": [true],
      "formattedValue": ["51"]
    }, {
      "time": "1359763200",
      "formattedTime": "Feb 2, 2013",
      "formattedAxisTime": "Feb 2, 2013",
      "value": [53],
      "hasData": [true],
      "formattedValue": ["53"]
    }, {
      "time": "1359849600",
      "formattedTime": "Feb 3, 2013",
      "formattedAxisTime": "Feb 3, 2013",
      "value": [53],
      "hasData": [true],
      "formattedValue": ["53"]
    }],
    "averages": []
  }
}

And this is how I'm calling for data:

console.log(results.default.timelineData[0].time);

I don't know what I'm doing wrong. Perhaps I am interpreting this JSON object incorrectly? Any help would be appreciated.

I've been struggling with this issue for quite a while now. When I try to console.log a JSON proprty, the following error shows up:

TypeError: Cannot read property 'timelineData' of undefined

This is what my JSON file looks like:

{
  "default": {
    "timelineData": [{
      "time": "1359676800",
      "formattedTime": "Feb 1, 2013",
      "formattedAxisTime": "Feb 1, 2013",
      "value": [51],
      "hasData": [true],
      "formattedValue": ["51"]
    }, {
      "time": "1359763200",
      "formattedTime": "Feb 2, 2013",
      "formattedAxisTime": "Feb 2, 2013",
      "value": [53],
      "hasData": [true],
      "formattedValue": ["53"]
    }, {
      "time": "1359849600",
      "formattedTime": "Feb 3, 2013",
      "formattedAxisTime": "Feb 3, 2013",
      "value": [53],
      "hasData": [true],
      "formattedValue": ["53"]
    }],
    "averages": []
  }
}

And this is how I'm calling for data:

console.log(results.default.timelineData[0].time);

I don't know what I'm doing wrong. Perhaps I am interpreting this JSON object incorrectly? Any help would be appreciated.

Share Improve this question asked Apr 14, 2018 at 20:37 HashimHashim 552 gold badges3 silver badges9 bronze badges 6
  • What is results set to? – pushkin Commented Apr 14, 2018 at 20:38
  • Works for me, maybe as Pushkin said the variable results is not set to the right value? Maybe timelineData isn't always set? – Maxim Khanov Commented Apr 14, 2018 at 20:40
  • Perhaps it actually is a JSON string rather than an object. Do you need to parse it with JSON.parse()? – Mark Commented Apr 14, 2018 at 20:40
  • @Mark_M Thanks for the help that worked for me. Dumb mistake on my part. – Hashim Commented Apr 14, 2018 at 20:59
  • @Hashim I added answer, hope it will work as per the expectation. Thanks. – Rohìt Jíndal Commented Apr 16, 2018 at 7:09
 |  Show 1 more ment

1 Answer 1

Reset to default 4

Observation :

results is looking like an array instead of object.

DEMO

var results = [{
	"default": {
		"timelineData": [{
			"time": "1359676800",
			"formattedTime": "Feb 1, 2013",
			"formattedAxisTime": "Feb 1, 2013",
			"value": [51],
			"hasData": [true],
			"formattedValue": ["51"]
		}, {
			"time": "1359763200",
			"formattedTime": "Feb 2, 2013",
			"formattedAxisTime": "Feb 2, 2013",
			"value": [53],
			"hasData": [true],
			"formattedValue": ["53"]
		}, {
			"time": "1359849600",
			"formattedTime": "Feb 3, 2013",
			"formattedAxisTime": "Feb 3, 2013",
			"value": [53],
			"hasData": [true],
			"formattedValue": ["53"]
		}],
		"averages": []
	}
}];

console.log(results.default.timelineData[0].time);

Hence, to access the time you should use like this :

var results = [{
	"default": {
		"timelineData": [{
			"time": "1359676800",
			"formattedTime": "Feb 1, 2013",
			"formattedAxisTime": "Feb 1, 2013",
			"value": [51],
			"hasData": [true],
			"formattedValue": ["51"]
		}, {
			"time": "1359763200",
			"formattedTime": "Feb 2, 2013",
			"formattedAxisTime": "Feb 2, 2013",
			"value": [53],
			"hasData": [true],
			"formattedValue": ["53"]
		}, {
			"time": "1359849600",
			"formattedTime": "Feb 3, 2013",
			"formattedAxisTime": "Feb 3, 2013",
			"value": [53],
			"hasData": [true],
			"formattedValue": ["53"]
		}],
		"averages": []
	}
}];

console.log(results[0].default.timelineData[0].time);

发布评论

评论列表(0)

  1. 暂无评论