我正在使用Python的app引擎项目,将数据从html页面(使用jQuery)发送到Python。
我正在使用这个jQuery插件将HTML表序列化为javascript对象:https://github.com/lightswitch05/table-to-json
function addFood() {
var table = $('#planTable').tableToJSON();
var info = {
"user": "{{user}}",
"plan": JSON.stringify(table)
};
$.ajax({
type: "POST",
url: "/addplan",
dataType: "json",
data: JSON.stringify(info)
})
.done(function(msg) {
alert(msg.text);
});
}
在Python中我有这个代码:
class AddPlanToDB(webapp2.RequestHandler):
def post(self):
info = json.loads(self.request.body) #retrive data from jQuery
print info
user = info['user']
template_values = {
'text': 'All done!'
}
self.response.out.write(json.dumps(template_values))
打印信息给我输出结果:
{u'plan': u'[{"Food":"Cheese, fontina","Grams":"100g","Pro":"25.6","Carbo":"1.6","Fat":"31.1","":""},{"Food":"Butter, salted","Grams":"50g","Pro":"0.4","Carbo":"0.0","Fat":"40.6","":""},{"Food":"Corn bran, crude","Grams":"200g","Pro":"16.7","Carbo":"171.3","Fat":"1.8","":""},{"Food":"","Grams":"Total:","Pro":"42.7","Carbo":"172.9","Fat":"73.5"}]', u'user': u'test@example.com'}
信息['用户'] 会返回有关用户的正确信息。 但我无法检索有关
的数据食物:
克
临
卡博:
脂肪:
有谁能告诉我如何用Python获取这些数据?
提前致谢并抱歉我的小英语。
答案 0 :(得分:1)
要获得有关饮食计划的信息,请执行以下操作
for item in json.loads(info['plan']):
print item['Food']
print item['Grams']
print item['Pro']
#etc
这是一个复制和粘贴示例
import json
data = {u'plan': u'[{"Food":"Cheese, fontina","Grams":"100g","Pro":"25.6","Carbo":"1.6","Fat":"31.1","":""},{"Food":"Butter, salted","Grams":"50g","Pro":"0.4","Carbo":"0.0","Fat":"40.6","":""},{"Food":"Corn bran, crude","Grams":"200g","Pro":"16.7","Carbo":"171.3","Fat":"1.8","":""},{"Food":"","Grams":"Total:","Pro":"42.7","Carbo":"172.9","Fat":"73.5"}]', u'user': u'test@example.com'}
for item in json.loads(data['plan']):
print item['Food']