如何从Python作为JSON返回值? [英] How to return value from Python as JSON?

查看:358
本文介绍了如何从Python作为JSON返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从如下所示的jQuery文件发送ajax请求,该请求需要JSON响应.

I sending ajax request from a jQuery file like below, which expects response in JSON.

jQuery.ajax({
    url: '/Control/getImageDetails?file_id='+currentId,
    type: 'GET',
    contentType: 'application/json',
    success: function (data){
        alert(data);
    }
 });
});

在Python上,我这样发送了对Ajax请求的响应:

On Python I sent response to the Ajax request as such:

 record = meta.Session.query(model.BannerImg).get(fid)
 return_info = [record.file_id, record.filename, record.links_to]
 return result_info

这将以纯文本形式返回参数,使其无法读取为不同的值.我相信从python发送响应作为JSON可以解决此问题.我以前曾经使用过JSON.如何以JSON格式返回响应?

This returns paramaters in plain text making it impossinle to read as different values. i believe sending off response from python as JSON solve this issue. I've nerver used JSON before. How can I return response as JSON?

推荐答案

return json.dumps(return_info)

主要问题是

 return_info = [record.file_id, record.filename, record.links_to]

因为JSON格式通常类似于

because JSON format is generally like

示例:

json.dumps({'file_id': record.file_id, 'filename': record.filename , 'links_to' : record.links_to})

,如果您使用alert(data)

因此,如果使用示例,请使用alert(data.file_id);

So use alert(data.file_id); if you use the example

这篇关于如何从Python作为JSON返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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