将python列表传递给javascript [英] passing python list to javascript

查看:817
本文介绍了将python列表传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将python列表传递给javascript函数,但是它不起作用...我认为Javascript函数会将其视为一个长字符串.这就是我要做的:

I am trying to pass a python list to a javascript function, but it doesn't work... I think the Javascript function will see it as one long string. This is what I do:

@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数据:

This is the python data:

["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"]

在Java语言中,我这样做:

In Javascript I do this:

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列表的每个字符,如下所示:

And the console prints every character of the python list on a seperate line, like this:

[ " 2个 0 1个 5 等等...

[ " 2 0 1 5 etc...

我无法格式化上面的console.log输出,但是每个字符都位于单独的行上.

I can't format the above console.log output, but every character is on a seperate line.

数组的长度也与总字符串的长度完全相同,所以我的结论是将python列表作为一个长字符串转换为Javascript ...

Also the length of the array is exactly the same as the length of the total string, so my conclusion is the python list is transformed to Javascript as one long string...

我希望有人能帮助我!

推荐答案

是的,您正在遍历字符串.这是因为json是字符串.这使得在不同的编程语言之间传递数据变得非常容易,因为字符串是几乎每种编程语言都实现的数据类型.但是,由于它是字符串,因此您需要将字符串解码为可用的格式/对象.在javascript中,您可以使用JSON.parse()方法.

You are right, you are looping through a string. This is because json are strings. This makes it pretty easy to pass data between different programming languages as strings are data types implemented in almost every programming language. However since it are strings you need to decode the string to a usable format/object. In javascript you can use the methodJSON.parse().

jsfiddle演示

var frompythonjsonstring ='["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"]';

macroCallBack(frompythonjsonstring);

function macroCallBack (str) {
        obj = JSON.parse(str)

        for (var i=0; i<obj.length; i++) {
            console.log(obj[i]);
        }
}

这篇关于将python列表传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆