【发布时间】:2013-06-14 13:13:23
【问题描述】:
我正在使用邮递员休息客户端发布此类数据。
{ 'name':"xyz",
'data':[{'age': 0, 'foo': 1}, {'age': 1, 'foo': 1}]
}
我以 unicode 格式获取数据,因此无法从此类数据中获取字典值。
我在做什么
def post(self, request):
d = request.DATA
# here prints right data if we "print d"
# but d is unicode so we could not access dictionary
for item in d['data]:
print item
如何将 unicode 转换为列表以及字典中的列表项,以便访问字典项。
注意我使用的是 django rest 框架。
【问题讨论】:
-
确保您不会不小心通过邮递员发布文字字符串。如果请求是有效的 JSON 并且 Content-Type 正确设置为
application/json,则访问request.DATA将返回解码的 python 原语(在这种情况下是值的字典) -
@TomChristie 我已经在表单数据中设置了这些值。
标签: python django django-rest-framework python-unicode