Django json 转储帮助
我有以下代码,需要帮助编码为 JSON 并将数据附加到文件 foo.json
这是我的代码:
user = request.user
print 'USER ID ', user.id // 83
sour = json.dumps({"toast" : [{"uid" : user.id }]})
print 'Sour Toast: ', sour.toast # I'm getting this error: AttributeError: 'str' object has no attribute 'toast'
基本上我想创建一个 json 文件,其中包含可以从我的前端访问的 user.id 值通过 jQuery。
如果有人可以帮助我解决我遇到的错误,或者提供有关修复此错误后该去哪里的任何提示,我将不胜感激。
I have the following code that I need help encoding to JSON and appending the data to a file, foo.json
Here is my code:
user = request.user
print 'USER ID ', user.id // 83
sour = json.dumps({"toast" : [{"uid" : user.id }]})
print 'Sour Toast: ', sour.toast # I'm getting this error: AttributeError: 'str' object has no attribute 'toast'
Basically I want to create a json file which contains the user.id value that can accessible from my front end through jQuery.
If anyone can help me with the error I'm getting or with any tips on where to go after i fix this error, I would greatly appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
json.dumps 返回一个字符串。因此,当您执行以下操作时:
Simple 会打印一个如下所示的字符串:
不是您可以从中获取单个值的实际对象或字典,只是一个您可以打印或写入文件的字符串或您想要对字符串执行的任何其他操作。看起来您想要打印此内容:
为此,您需要执行以下操作:
json.dumps returns you a string. So when you do:
Simple prints a string that looks like this:
Not an actual object or dict you can get individual values from, just a string you can print or write to a file or whatever else you want to do with strings. It looks like you want to print this:
To do that you'd want to do the following: