当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Transforming db data to json in web2py for visualization with flot.js

问题描述:

I'm having problem to get a valid json string for flot.js visualisation from db data in web2py.

This is my controller action:

def getdata():

return dict(data=db().select(db.realtimedata.id, db.realtimedata.FlowRate, limitby=(0, 3), orderby=~db.realtimedata.id))

Generic json view returns this json string for getdata action:

{"data": [{"id": 1, "FlowRate": 373}, {"id": 2, "FlowRate": 379}, {"id": 3, "FlowRate": 376}]}

How can i get this json string?

[[1,373],[2,379],[3,376]]

网友答案:
import json

def getdata():
    rows = db().select(db.realtimedata.id, db.realtimedata.FlowRate,
                       limitby=(0, 3), orderby=~db.realtimedata.id)
    return json.dumps([[r.id, r.FlowRate] for r in rows])

Note, the above returns a string directly, so no view will be called.

分享给朋友:
您可能感兴趣的文章:
随机阅读: