我试图将一个Python列表传递给JavaScript函数,但是并不起作用,我认为Javascript函数会把它看作一个长字符串。这是我所做的事情:
在Javascript中,我会这样做:
@webiopi.macro
def calcSunriseSunsetOneYear():
o=ephem.Observer()
o.lat='51.21828'
o.long='3.94958'
s=ephem.Sun()
d=datetime.date.today()
t=timedelta(days=1)
d=d+t
o.date=d
riseTime=[]
setTime=[]
s.compute()
for x in range (0,365):
o.date=o.date+1
riseTime.append(str(ephem.localtime(o.next_rising(s))))
setTime.append(str(ephem.localtime(o.next_setting(s))))
return(json.dumps(riseTime))
这是Python数据:
["2015-03-22 06:43:02.000006", "2015-03-23 06:40:46", "2015-03-24 06:38:31.000001", "2015-03-25 06:36:15.000002", "2015-03-26 06:33:59.000003", "2015-03-27 06:31:44.000004", "2015-03-28 06:29:28.000005", "2015-03-29 07:27:13.000006", "2015-03-30 07:24:57", "2015-03-31 07:22:42.000001", "2015-04-01 07:20:27.000002", "2015-04-02 07:18:13.000003", "2015-04-03 07:15:58.000004", "2015-04-04 07:13:44.000005", "2015-04-05 07:11:31.000006", "2015-04-06 07:09:17", "2015-04-07 07:07:04.000001", "2015-04-08 07:04:52.000002", "2015-04-09 07:02:40.000003", "2015-04-10 07:00:28.000004"]
在Javascript中,我会这样做:
var printChart = function macroCallBack(macro, args, chartInfo) {
document.getElementById("chartInfo").innerHTML=chartInfo;
var arLength=chartInfo.length;
console.log("arLength is: ",arLength);
for (var i=0; i<arLength; i++) {
console.log(chartInfo[i]);
}
}
控制台将Python列表中的每个字符打印在单独的一行,如下所示:
[ " 2 0 1 5 等等...
我无法格式化上面的console.log输出,但每个字符都在单独的一行。
此外,数组的长度与字符串的总长度完全相同,因此我的结论是Python列表被转换为Javascript作为一个长字符串...
希望有人可以帮助我!