gpt4 book ai didi

python - 如何打印出 JSON 输出的确切字段/字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:52 25 4
gpt4 key购买 nike

我正在尝试过滤 GET 请求中获得的所有结果。我想要的输出只是获取 summary: 、 key: 和 self: 。但我得到了很多 Json 数据。我尝试用谷歌搜索如何做到这一点,但毫无结果。

这是我的代码:注释行是我尝试过的代码。

import requests
import json
import re
import sys
url ="--------"
auth='i.g--t----------', 'X4------'
r = requests.get(url, auth=(auth))
data = r.json()
#print( json.dumps(data, indent=2) )
#res1 = " ".join(re.split("summary", data))
#print ("first string result: ", str(res1))
#json_str = json.dumps(data)
#resp = json.loads(json_str)
#print (resp['id'])
#resp_dict = json.loads(resp_str)
#resp_dict.get('name')
#print('dasdasd', json_str["summary"])

我使用此代码获取的 Get Api 输出的示例。 print( json.dumps(data, indent=2) )

 {
"id": "65621",
"self": "https://bboxxltd.atlassian.net/rest/api/2/issue/65621",
"key": "CMS-5901",
"fields": {
"summary": "new starter: Edoardo Bologna",
"customfield_10700": [
{
"id": "2",
"name": "BBOXX Rwanda HQ",
"_links": {
"self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/organization/2"
}
}
},
"inwardIssue": {
"id": "65862",
"key": "BMT-2890",
"self": "https://bboxxltd.atlassian.net/rest/api/2/issue/65862",
"fields": {
"summary": "ERP Databases access with Read Only",
"status": {
"self": "https://bboxxltd.atlassian.net/rest/api/2/status/10000",
"description": "",
"iconUrl": "https://bboxxltd.atlassian.net/",
"name": "To Do",
"id": "10000",
"statusCategory": {
"self": "https://bboxxltd.atlassian.net/rest/api/2/statuscategory/2",
"id": 2,
"key": "new",
"colorName": "blue-gray",
"name": "To Do"
}
},
"priority": {
"self": "https://bboxxltd.atlassian.net/rest/api/2/priority/4",
"iconUrl": "https://bboxxltd.atlassian.net/images/icons/priorities/low.svg",
"name": "Low",

我的错误是:

Traceback (most recent call last):
File "c:/Users/IanJayloG/Desktop/Python Files/Ex_Files_Learning_Python/Exercise Files/Test/Untitled-1.py", line 17, in <module>
print('dasdasd', data["summary"])
KeyError: 'summary'
PS C:\Users\IanJayloG\Desktop\Python Files\Ex_Files_Learning_Python\Exercise Files> & C:/Users/IanJayloG/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/IanJayloG/Desktop/Python Files/Ex_Files_Learning_Python/Exercise Files/Test/Untitled-1.py"
Traceback (most recent call last):
File "c:/Users/IanJayloG/Desktop/Python Files/Ex_Files_Learning_Python/Exercise Files/Test/Untitled-1.py", line 17, in <module>
print('dasdasd', json_str["summary"])
TypeError: string indices must be integers

最佳答案

关于您的错误消息的问题

  print('dasdasd', json_str["summary"])
TypeError: string indices must be integers

您尝试访问字符串(变量json_str)上的命名字段summary,这不起作用,因为字符串没有可按名称访问的字段。如果对字符串使用索引 [] 运算符,则只能提供整数或范围来从该字符串中提取单个字符或序列。这显然不是你想要的。

selfkey 位于 JSON 文档的顶层,而 summary 位于 fields 下。这应该可以做到,无需应用任何额外的转换:

import requests

r = requests.get(url, auth=(auth))
data = r.json()

data_summary = data['fields']['summary']
data_self = data['self']
data_key = data['key']

关于python - 如何打印出 JSON 输出的确切字段/字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57700107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com