2013-03-17 63 views
1

免责声明:我完全不熟悉解析和访问JSON,我只是开始获得django的绳索。使用Django访问JSON数据

详情:

我试图访问内部的JSON对象我在Django模板获得的一些数据:

例如JSON:

{"entry":[ 
{"id":"1234567","hash":"1234567", 
..."name":{"givenName":"John","familyName":"Doel","formatted":"John H. Doe"}, 
..."currentLocation":"Los Angeles, CA", 
..."emails":[{"primary":"true","value":"[email protected]"}], 
..."accounts":[ 
    ...{"domain":"facebook.com","url":"http:\/\/www.facebook.com\/john.doe",..."shortname":"facebook"}, 
    ...{"domain":"twitter.com","display":"@jdoe411","url":"http:\/\/twitter.com\/jdoe411",..."shortname":"twitter"}], 
..."urls":[{"value":"http:\/\/www.example.com","title":"example url"},]} 
]} 

得到Django的模板标签:

@register.inclusion_tag('includes/gravatar_links.html') 
def gravatar_links(email, first_name): 
    url = "http://www.gravatar.com/" + hashlib.md5(email.lower()).hexdigest() 
    url += '.json' 

    response = urllib2.urlopen(url) 
    interm = simplejson.load(response) 
    data = simplejson.dumps(interm) 
    name = first_name 

return {'gravatar_links': {'name': name, 'data': data}} 

我刚才打电话{{ gravatar_links.name }}{{ gravatar_links.data }},我可以看到用户的名称和JSON数据描述。

目标:

我想访问特定的键在JSON对象,即"accounts""urls",以及自定义输出,(例如)"Follow {{ gravatar_links.name }} on <a href="{{ gravatar_links.data.accounts.url }}">{{ gravatar_links.data.accounts.shortname }}</a> and see their work at <a href="{{ gravatar_links.data.urls.url }}">{{ gravatar_links.data.urls.title }}</a>"

在理想情况下会产生:

"Follow John on Twitter and Facebook and see their work at example url"

问题:

  1. 我应该只使用django/python来做到这一点,或者这是更好的jQuery处理?
  2. 如果"accounts"以列表形式出现,如何将它输出为逗号分隔列表?
  3. 使用这种方法应该注意哪些安全问题?
+0

我可能只是使用Python,这和保存的结果,因为它是不太可能改变。数据库访问比JSON请求更快...多数民众赞成只是我的2C虽然 – 2013-03-17 19:10:36

回答

0

模板标签应该是:

@register.inclusion_tag('includes/gravatar_links.html') 
def gravatar_links(email, first_name): 
    url = "http://www.gravatar.com/" + hashlib.md5(email.lower()).hexdigest() 
    url += '.json' 

    response = urllib2.urlopen(url) 
    interm = simplejson.load(response) 
    accounts_list = interm["entry"][0]["accounts"] 
    data = simplejson.dumps(interm) 

    return {'gravatar_links': {'accounts': accounts_list, 'name': first_name, 'data': data}} 

,然后访问JSON对象,我们可以继续使用类似accounts_list = interm["entry"][0]["accounts"]除非"urls"的元素,我们不得不urls_list = interm["entry"][0]["urls"]

与更多的元素一个可能的dictionary[element: {key1=>value1, ...}, {key2=>value2, ...}]只是在您的模板中设置for循环并遍历这样的项目:

and follow {{ gravatar_links.name }} on 

{% for o in gravatar_links.accounts %} 
    {% if not forloop.first and not forloop.last %}, {% endif %} 
    {% if forloop.last %} and {% endif %} 

    <a href="{{ o.url }}">{{ o.shortname }}</a> 

    {% if forloop.last %}.{% endif %} 
{% endfor %} 

额外if forloop语句将谱写我们的输出:

and follow John on facebook and twitter.

超过2个网址: and follow John on facebook, linkedin, and twitter.

0

对于以逗号分隔的列表中,您使用来自Django的(https://docs.djangoproject.com/en/dev/ref/templates/builtins/)的join模板标签:

{{ accounts|join:", " }} 

如果你想要去更看中的,你可以写自己的模板标签(并不困难,因为它似乎!)。

+0

谢谢,但你是对的我需要写一个模板标签,以得到这个工作,我想如何 – frankV 2013-03-17 20:44:18