我正在尝试使用以下脚本来执行输出JSON的API查询。我正在迭代JSON以打印所有JSON对象的特定字段(名称)。我的脚本打印所有项目(名称,分类,详细信息等)而不是值。
脚本:
import requests
import json
query_url = 'https://IP-address/api/request'
data = {"query":"days>50", "type":"json", "size":3}
response = requests.post(query_url, json=data, verify=False)
json_data = json.loads(response.text)
for name in json_data[0]:
print (name)
JSON:
[
{
"name": "Android",
"classification": "public",
"detail": "Tag1, Tag2, Tag3",
"days": 70,
"date_last": "2017-07-21 05:48:07 AM"
},
{
"name": "iPhone",
"classification": "public",
"detail": "Tag4, Tag5, Tag6",
"days": 75,
"date_last": "2017-07-21 05:48:07 AM"
},
{
"name": "Microsoft",
"classification": "public",
"detail": "Tag3, Tag8, Tag9",
"days": 90,
"date_last": "2017-07-21 05:48:07 AM"
}
]
答案 0 :(得分:2)
如果要迭代json_data
并打印每个名称,您需要按键访问每个字典值:
for d in json_data:
print(d['name'])
或者,如果您只想打印特定字典的“名称”,例如JSON数据中的第一个,迭代不是必需的:
print(json_data[0]['name'])
假设您希望以特定方式打印输出,可以使用str.format
:
for d in json_data:
print("Name: {} Days: {}".format(d['name'], d['days']))
答案 1 :(得分:2)
我的感觉是问题的一部分是我们不清楚我们在看什么...所以我建议我们花一点时间来了解一下:
如果我们只是要求Python向我们展示json_data中的每个项目,我们会看到Python已经在字典列表中的每个字典中读取并将为我们显示它们。
In [23]: for item in json_data:
...: print(item)
...:
{'days': 70, 'classification': 'public', 'date_last': '2017-07-21 05:48:07 AM', 'name': 'Android', 'detail': 'Tag1, Tag2, Tag3'}
{'days': 75, 'classification': 'public', 'date_last': '2017-07-21 05:48:07 AM', 'name': 'iPhone', 'detail': 'Tag4, Tag5, Tag6'}
{'days': 90, 'classification': 'public', 'date_last': '2017-07-21 05:48:07 AM', 'name': 'Microsoft', 'detail': 'Tag3, Tag8, Tag9'}
从那里开始,由于每个项目都是字典,我们下一步就是要求Python只提取与名称密钥相关的值:
In [21]: for item in json_data:
...: print(item['name'])
...:
...:
Android
iPhone
Microsoft
答案 2 :(得分:0)
您应该访问密钥'名称'
的值下面是代码
<md-card class="chart-container">
<div class="example-container mat-elevation-z8">
<md-table #table [dataSource]="dataSource">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- ID Column -->
<ng-container cdkColumnDef="userId">
<md-header-cell *cdkHeaderCellDef> ID </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.id}} </md-cell>
</ng-container>
<!-- Progress Column -->
<ng-container cdkColumnDef="progress">
<md-header-cell *cdkHeaderCellDef> Progress </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.progress}}% </md-cell>
</ng-container>
<!-- Name Column -->
<ng-container cdkColumnDef="userName">
<md-header-cell *cdkHeaderCellDef> Name </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.name}} </md-cell>
</ng-container>
<!-- Color Column -->
<ng-container cdkColumnDef="color">
<md-header-cell *cdkHeaderCellDef>Color</md-header-cell>
<md-cell *cdkCellDef="let row" [style.color]="row.color"> {{row.color}} </md-cell>
</ng-container>
<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row> <----- Heare is the problem
<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row> <----- Heare is the problem
</md-table>
</div>
</md-card>
Uncaught Error: Template parse errors:
Can't bind to 'cdkHeaderRowDef' since it isn't a known property of 'md-header-row'.
1. If 'md-header-row' is an Angular component and it has 'cdkHeaderRowDef' input, then verify that it is part of this module.
2. If 'md-header-row' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
</ng-container>
<md-header-row [ERROR ->]*cdkHeaderRowDef="displayedColumns"></md-header-row>
<md-row *cdkRowDef="let row; col"): ng:///AppModule/AdminProductsComponent.html@60:31
Property binding cdkHeaderRowDef not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("
</ng-container>
[ERROR ->]<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
<md-row *cdkRowDe"): ng:///AppModule/AdminProductsComponent.html@60:16
Can't bind to 'cdkRowDefColumns' since it isn't a known property of 'md-row'.
1. If 'md-row' is an Angular component and it has 'cdkRowDefColumns' input, then verify that it is part of this module.
2. If 'md-row' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. (" <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
<md-row [ERROR ->]*cdkRowDef="let row; columns: displayedColumns;"></md-row>
</md-table>
</di"): ng:///AppModule/AdminProductsComponent.html@61:24
Property binding cdkRowDefColumns not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". (" <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
[ERROR ->]<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>
</md-table>
"): ng:///AppModule/AdminProductsComponent.html@61:16
at syntaxError (http://localhost:4200/vendor.bundle.js:86242:34)
at TemplateParser.parse (http://localhost:4200/vendor.bundle.js:97363:19)
at JitCompiler._compileTemplate (http://localhost:4200/vendor.bundle.js:111515:39)
at http://localhost:4200/vendor.bundle.js:111435:62
at Set.forEach (native)
at JitCompiler._compileComponents (http://localhost:4200/vendor.bundle.js:111435:19)
at http://localhost:4200/vendor.bundle.js:111322:19
at Object.then (http://localhost:4200/vendor.bundle.js:86231:148)
at JitCompiler._compileModuleAndComponents (http://localhost:4200/vendor.bundle.js:111321:26)
at JitCompiler.compileModuleAsync (http://localhost:4200/vendor.bundle.js:111250:37)