在python中为应用程序引擎生成json [英] Generating json in python for app engine

查看:89
本文介绍了在python中为应用程序引擎生成json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python有点新,我想知道在循环中生成json的最好方法是什么。我可以在循环中混合一堆字符串,但我相信有更好的方法。这里有一些更具体的细节。我在python中使用app引擎创建一个返回json作为响应的服务。

I am somewhat new to python and I am wondering what the best way is to generate json in a loop. I could just mash a bunch of strings together in the loop, but I'm sure there is a better way. Here's some more specifics. I am using app engine in python to create a service that returns json as a response.

例如,假设有人从服务请求了一列用户记录。服务查询记录后,它需要为找到的每个记录返回json。可能是这样的:

So as an example, let's say someone requests a list of user records from the service. After the service queries for the records, it needs to return json for each record it found. Maybe something like this:

{records: 
{record: { name:bob, email:blah@blah.com, age:25 } },
{record: { name:steve, email:blah@blahblah.com, age:30 } }, 
{record: { name:jimmy, email:blah@b.com, age:31 } }, 
}

原谅我格式不正确的json。感谢您的帮助。

Excuse my poorly formatted json. Thanks for your help.

推荐答案


我的问题是如何添加到
字典动态?因此,在我的记录列表中记录
记录,并在字典中添加
记录。

My question is how do I add to the dictionary dynamically? So foreach record in my list of records, add a record to the dictionary.

您可能会希望创建一个字典列表。

You may be looking to create a list of dictionaries.

records = []
record1 = {"name":"Bob", "email":"bob@email.com"}
records.append(record1)    
record2 = {"name":"Bob2", "email":"bob2@email.com"}
records.append(record2)

然后在应用引擎中,使用上面的代码导出记录作为json。

Then in app engine, use the code above to export records as json.

这篇关于在python中为应用程序引擎生成json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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