Google App Script - 访问JSON对象

时间:2014-04-14 05:43:52

标签: javascript json google-apps-script

我正在尝试使用谷歌应用程序脚本从JSON对象访问某些数据字段,并且我收到以下错误:无法读取属性" dateTime"来自undefined。

var steps = UrlFetchApp.fetch("http://api.fitbit.com/1/user/-/activities/steps"
      + "/date/" + dateString +
                                 "/1d/15min.json", options);

和步骤是 {"活动步骤":[{"日期时间":" 2014年3月28日""值":" 5336& #34;}],"活动步骤-盘中" {"数据集":[{"时间":" 00:00:00&# 34;,"值":0},{"时间":" 00:15:00""值":0}, {"时间":" 02:45:00""值":0},{"时间":" 03 :00:00""值":0},{"时间":" 03:15:00""值&#34 ;:549},{"时间":" 03:30:00""值" 333},{"时间&#34 ;: " 20:45:00""值":0},{"时间":" 21:00:00"&# 34;值":0},{"时间":" 21:15:00""值":0},{"时间":" 21:30:00""值":0},{"时间":" 21:45:00&# 34;,"值":0},{"时间":" 22:00:00""值":0}, {"时间":" 22:15:00""值":0},{"时间":" 22 :30:00""值":0},{"时间":" 22:45:00""值&#34 ;:0},{"时间":" 23:00: 00""值":0},{"时间":" 23:15:00""值":0 },{"时间":" 23:30:00""值":0},{"时间":&#34 ; 23:45:00""值":0}]," datasetInterval":15," datasetType":"分钟&#34 ;}}

我是通过MailApp.sendEmail('xyz@gmail.com','Data',steps);

得到的

并通过执行以下代码产生错误:

var steps2 = JSON.parse(steps.getContentText()); 
var blah = steps2.activities-steps[0].dateTime;

var steps2 = JSON.parse(steps); 
var blah = steps2.activities-steps[0].dateTime;

谢谢!

1 个答案:

答案 0 :(得分:1)

你不能用点符号表示连字符。试试steps2['activities-steps'][0].dateTime