【问题标题】:printing json data in format of key value pair in django在 django 中以键值对的格式打印 json 数据
【发布时间】:2017-12-01 10:46:58
【问题描述】:

我正在尝试以键值对的格式打印 json 数据,以便我可以在我的 html 模板中呈现它们。

def ecpd(request):
    r= requests.get('http://jira.xxx.xxx.com/rest/api/2/issue/key-XXX',auth=HTTPBasicAuth('user','pass'),headers = {'Content-Type' : 'application/json'})
    jsonDict = json.loads(r.content)
return HttpResponse(jsonDict['fields'])

作为回应,我只得到“字段”中的键列表。 比如:customfield_10070customfield_10071customfield_10072customfield_10073customfield_13221customfield_10074customfield_13220customfield_10075。 我需要一个 dict 格式的键值对。

【问题讨论】:

  • 值是多少?

标签: python json django


【解决方案1】:

HttpResponse,如果传递了一个可迭代对象,将对其进行迭代以构建响应内容。 dict 是可迭代的,迭代它实际上是迭代字典的键。根据您的问题描述,我假设 jsonDict['fields'] 确实是 dict

如果您想将此字典返回为json(即使用ajax 调用视图),那么您必须将其转储回json 并返回正确的“json”响应。您可以手动完成(转储回 json 并设置正确的内容类型),或使用内置 JsonResponse (Django >= 1.7)

r = requests.get('http://jira.xxx.xxx.com/rest/api/2/issue/key-XXX',auth=HTTPBasicAuth('user','pass'),headers = {'Content-Type' : 'application/json'})
jsonDict = r.json() # requests shortcut
# Django >= 1.7
return JsonResponse(jsonDict["fields"])
# Django < 1.7
data = json.dumps(jsonDict["fields"])
return HttpResponse(data, content_type="application/json")

现在,如果您想要在 Django 模板中呈现它,只需将其传递给模板的上下文即可:

r = requests.get('http://jira.xxx.xxx.com/rest/api/2/issue/key-XXX',auth=HTTPBasicAuth('user','pass'),headers = {'Content-Type' : 'application/json'})
jsonDict = r.json() # requests shortcut
return render(request, "path/to/your/template.html", {"data":jsonDict["fields"]})

在你的模板中:

<dl>
{% for k, v in data %}:
  <dt>{{ k }}</dt>
  <dd>{{ v }}</dd>
{% endfor %}
</dl>

【讨论】:

    【解决方案2】:

    之前的 Django 1.7

    使用

    def ecpd(request):
        r = requests.get('http://jira.xxx.xxx.com/rest/api/2/issue/key-XXX',auth=HTTPBasicAuth('user','pass'),headers = {'Content-Type' : 'application/json'})
        response_data = r.content
        return HttpResponse(json.dumps(response_data), content_type="application/json")
    

    从 Django 1.7 开始

    from django.http import JsonResponse
    
    #rest of the views
    def ecpd(request):
        r = requests.get('http://jira.xxx.xxx.com/rest/api/2/issue/key-XXX',auth=HTTPBasicAuth('user','pass'),headers = {'Content-Type' : 'application/json'})
        response_data = r.content
        return JsonResponse(response_data)
    

    【讨论】:

      【解决方案3】:
      def ecpd(request):
           r= requests.get('http://jira.xxx.xxx.com/rest/api/2/issue/key-XXX',auth=HTTPBasicAuth('user','pass'),headers = {'Content-Type' : 'application/json'})
          if r.status_code == 200:
               jsonDict = r.json()
               return HttpResponse(jsonDict)
      

      【讨论】:

      • 请在您的帖子中解释它是如何工作的以及为什么它是一个解决方案。
      • 嗨,阿曼。我试过它显示同样的东西。我实际上想遍历“字段”,这些字段又具有键值对,例如 {summary:xxx , desc : xxx} 并将它们作为 json 对象返回。
      猜你喜欢
      • 2019-02-10
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多