在django中处理JSON的最佳方式 [英] best way to deal with JSON in django
问题描述
我从服务器获取JSON源,今天将其转换为python对象,从而转换为django视图。我们现在正在更新我们的网站。因此
- 浏览器客户端应该使用jQuery解析json
- 我们也会有一个adobe-air将直接使用JSON的应用程序
然而,我不是很热衷于将我的后端服务器直接暴露给浏览器/ adobe客户端。如何通过django最好的方法?任何现有的django应用程序?
认罪
django-newbie
使用某些 Django的内置元素,但我一直发现< a href =http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.3/docs/index.html =noreferrer> SimpleJSON 使事情变得更加容易。 / p>
为什么?通过直接序列化,您不想显示所有内容。所以用内置的方法,你必须削减很多。使用SimpleJSON,您构建了一个dict,只需填写您想要显示的内容并将其通过SimpleJSON lib进行抽象。对于公开API,我发现包含的安全性比排除性更强。
对于消费数据来说,它也更为多用途,因为您的客户端不会成为django网站,它是一个AIR应用程序,有自己的想法如何格式化数据(即使在像JSON这样的规范中也可能会有差异)。
哦,记住JSON中没有日期类型。 (我只提到它,因为它使我过去痛苦)
编辑:(感谢Cide)Django在 django.utils.simplejson中发送SimpleJSON
可能不会永远存在。无论如何,您可以从 Pypi 单独下载
I am getting a JSON feed from a server and today I convert it to python object and thus to django view. We are now making an update of our site. whereby
- the browser client should parse json using jQuery
- also we will have a adobe-air app which will consume JSON directly
However I am not so keen on exposing my back-end server directly to browser/adobe client. How best way to go via django? any existing django-app?
regards django-newbie
You can use certain built-in elements of Django but I've always found that SimpleJSON makes things so much easier.
Why? With straight serialisation, you don't want to show everything. So with the built-in methods, you have to cut a lot out. With SimpleJSON, you built a dict, fill it with only what you want shown and pump it through the SimpleJSON lib. I find inclusion a lot more secure than exclusion when it comes to exposing APIs.
It's also a lot more versatile for consuming data as your client isn't going to be a django site, it's an AIR app with its own ideas about how to format data (even within a spec like JSON there can and probably will be differences).
Oh and remember that there isn't a date type in JSON. (I only mention it because it caused me pain in the past)
Edit: (Thanks Cide) Django ships SimpleJSON in django.utils.simplejson
but it might not be there forever. Regardless, you can download it separately from Pypi
这篇关于在django中处理JSON的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!