我试图打印一个API调用的结果,它返回的JSON结果嵌套得比较深。我用python2.7和django1.11做这个项目。在
我有以下内容视图.py功能:
def nlu_analysis(request):
if request.method == 'POST':
text2send = request.POST.get('text2send')
natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2017-02-27',
username='####',
password='####')
response = natural_language_understanding.analyze(
text=text2send,
features=[features.Entities(), ..., features.SemanticRoles()])
return render(request, 'watson_nlu/analysis.html', {'data': response})
当我在.html文件中使用以下模板代码时:
^{pr2}$要解析和显示具有一个嵌套级别的JSON,请执行以下操作:
'keywords': [{
'relevance': 0.946673,
'text': 'eyes'
}]
一切都很好,它显示“眼睛”和0.946673,如预期。在
我无法找到合适的语法来获取“愤怒”、“喜悦”等。嵌套得更深的结果如下:
{
'emotion': {
'document': {
'emotion': {
'anger': 0.195192,
'joy': 0.082313,
'sadness': 0.644314,
'fear': 0.207166,
'disgust': 0.103676
}
}
}
实现这一目标最有效的方法是什么?在
绝对不是:
<p>Anger - {{ data['emotion.document.template.anger'] }}</p>
向新手表示感谢,并感谢你的帮助。在
您可以使用
.
访问dict的dict,因此您的问题的解决方案是data.emotion.document.emotion.anger
相关问题 更多 >
编程相关推荐