解析作为 POST 参数传递的字典列表

发布于 2024-09-06 02:41:55 字数 552 浏览 5 评论 0原文

我有一个如下所示的 python 字典列表:

sandwiches = [
    {'bread':'wheat', 'topping':'tomatoes', 'meat':'bacon'},
    {'bread':'white', 'topping':'peanut butter', 'meat':'bacon'},
    {'bread':'sourdough', 'topping':'cheese', 'meat':'bacon'}
]

我想将其作为 POST 参数传递给另一个 Django 应用程序。客户端应用程序需要执行哪些操作才能迭代列表?

我想做类似的事情:

for sandwich in request.POST['sandwiches']:
    print "%s on %s with %s is yummy!" % (sandwich['meat'], sandwich['bread'], sandwich['topping'])

但是当我的数据到达我的客户端应用程序时,我似乎没有字典列表。

I have a list of python dictionaries that look like this:

sandwiches = [
    {'bread':'wheat', 'topping':'tomatoes', 'meat':'bacon'},
    {'bread':'white', 'topping':'peanut butter', 'meat':'bacon'},
    {'bread':'sourdough', 'topping':'cheese', 'meat':'bacon'}
]

I want to pass this as a POST parameter to another Django app. What does the client app need to do to iterate through the list?

I want to do something like:

for sandwich in request.POST['sandwiches']:
    print "%s on %s with %s is yummy!" % (sandwich['meat'], sandwich['bread'], sandwich['topping'])

But I don't seem to have a list of dicts when my data arrives at my client app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阪姬 2024-09-13 02:41:55

您没有说明如何发布到应用程序。我的建议是将字典序列化为 JSON,然后简单地发布它。

import json, urllib, urllib2
data = json.dumps(sandwiches)
urllib2.urlopen(myurl, urllib.urlencode({'data': data}))

...在接收器上:

data = request.POST['data']
sandwiches = json.loads(data)

You don't say how you're POSTing to the app. My advice would be to serialize the dictionaries as JSON, then simply POST that.

import json, urllib, urllib2
data = json.dumps(sandwiches)
urllib2.urlopen(myurl, urllib.urlencode({'data': data}))

... and on the receiver:

data = request.POST['data']
sandwiches = json.loads(data)
極樂鬼 2024-09-13 02:41:55

我将使用 JSON 库将字典数组序列化为单个字符串。将此字符串作为 POST 参数发送,并使用相同的库将该字符串解析回 Django 应用程序中的 python 数据类型。

http://docs.python.org/library/json.html

I would use the JSON libraries to serialize your array of dicts into a single string. Send this string as a POST param, and parse the string back into the python datatypes in the Django app using the same library.

http://docs.python.org/library/json.html

三人与歌 2024-09-13 02:41:55

首先制作列表的字符串

import ast, requests

# request send code
dev_list = [{"1": 2}, {"2": 4}]

str_list = str(dev_list) # '[{"1": 2}, {"2": 4}]'

data = {"a" : "mynema",
        'str_list': str_list
       }

requests.post(url, data )

#request receive side
data = request.data

dev_list = data.get('str_list')

dev_list = ast.literal_eval(dev_list)

#dev_list --> [{"1": 2}, {"2": 4}]

现在您收到字典列表。

firstly make string of the list

import ast, requests

# request send code
dev_list = [{"1": 2}, {"2": 4}]

str_list = str(dev_list) # '[{"1": 2}, {"2": 4}]'

data = {"a" : "mynema",
        'str_list': str_list
       }

requests.post(url, data )

#request receive side
data = request.data

dev_list = data.get('str_list')

dev_list = ast.literal_eval(dev_list)

#dev_list --> [{"1": 2}, {"2": 4}]

Now you receive list of dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文