Jinja解析嵌套的JSON [英] Jinja parsing a nested JSON
问题描述
这是我第一次尝试使用Jinja或与Python相关的任何东西,但是我们的供应商已实现了一个使用它的新工具,所以就在这里.
This is my first attempt at using Jinja or anything Python related, but our vendor has a new tool implemented that uses it, so here we are.
作为测试,我正在使用免费的API作为数据源,并希望通过工具的Jinja对其进行转换. API为 http://www.floatrates.com/daily/usd.json . ..
As a test, I'm using a free API as a data source and want to convert it through the tool's Jinja. API is http://www.floatrates.com/daily/usd.json...
{"eur":{
"code":"EUR",
"alphaCode":"EUR",
"numericCode":"978",
"name":"Euro",
"rate":0.90140326265491,
"date":"Tue, 21 Jan 2020 12:00:01 GMT",
"inverseRate":1.1093813850359},
"gbp":{
"code":"GBP",
"alphaCode":"GBP",
"numericCode":"826",
"name":"U.K. Pound Sterling",
"rate":0.76735727130961,
"date":"Tue, 21 Jan 2020 12:00:01 GMT",
"inverseRate":1.3031739417721},
"jpy":{
"code":"JPY",
"alphaCode":"JPY",
"numericCode":"392",
"name":"Japanese Yen",
"rate":109.99188913429,
"date":"Tue, 21 Jan 2020 12:00:01 GMT",
"inverseRate":0.009091579459819}...
我的目标是遍历内容(此工具在content属性中具有响应的正文),并为每个内容获取代码和最终费率.我知道一旦成功获得这些值该怎么办...这就是我到目前为止所拥有的
My goal is to iterated through the content (which this tool has the response's body in the content property) and grab the code and eventual rate for each one. I know what to do once I can successfully get those values... Here is what I have so far
{% set Collection = a.content|to_json %}
{% for obj in Collection %}
{% set CurrentObject = obj %}
{{ Collection[CurrentObject].code }}
{% endfor %}
但是,这似乎不起作用...如果我执行a.content | to_json ['eur'].code,则将EUR用作值,但是我尝试不对每次迭代进行硬编码.
This doesn't appear to work however... if I do a.content|to_json['eur'].code then I get the EUR as a value, but I'm trying to not hardcode for each iteration.
谢谢.
推荐答案
我认为您不希望to_json
在那里进行过滤.这是为了生成数据结构的JSON表示形式.你不想要那个您实际上想要的是数据结构.
I don't think you want that to_json
filter there. That's for producing a JSON representation of a data structure. You don't want that; you actually want the data structure.
如果您的问题中的数据位于content
属性中,则可以编写如下内容:
If you have the data in your question in the content
property, you could write something like this:
{% for code in content %}
{{ code }} {{ content[code].rate }}
{% endfor %}
这会给你:
eur 0.90140326265491
gbp 0.76735727130961
jpy 109.99188913429
这是我要测试的代码:
import jinja2
content = {
"eur": {
"code": "EUR",
"alphaCode": "EUR",
"numericCode": "978",
"name": "Euro",
"rate": 0.90140326265491,
"date": "Tue, 21 Jan 2020 12:00:01 GMT",
"inverseRate": 1.1093813850359,
},
"gbp": {
"code": "GBP",
"alphaCode": "GBP",
"numericCode": "826",
"name": "U.K. Pound Sterling",
"rate": 0.76735727130961,
"date": "Tue, 21 Jan 2020 12:00:01 GMT",
"inverseRate": 1.3031739417721,
},
"jpy": {
"code": "JPY",
"alphaCode": "JPY",
"numericCode": "392",
"name": "Japanese Yen",
"rate": 109.99188913429,
"date": "Tue, 21 Jan 2020 12:00:01 GMT",
"inverseRate": 0.009091579459819,
},
}
t = jinja2.Template('''{% for code in content %}
{{ code }} {{ content[code].rate }}
{% endfor %}''')
print(t.render(content=content))
这篇关于Jinja解析嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!