【发布时间】:2021-04-19 13:23:18
【问题描述】:
作为对请求的响应,我有以下输出:
{
[
"1545904980", //val1
"0.058", //val2
"0.049", //val3
"0.058", //val4
"0.049", //val5
"0.018", //val6
"0.000945" //val7
],
[
"1545904920",
"0.058",
"0.072",
"0.072",
"0.058",
"0.103",
"0.006986"
]
}
假设我想访问 val3 并因此访问“0.049”,这就是我使用的:
test = requests.get("url")
# to load response into json we must convert response to string
test_list = str(test.json())
#before loading response into json we must convert '' string to "" type string.
s_test = test_list.replace("\'", "\"")
test_data = json.loads(s_test)
for i in test_data:
for j in i:
print(i[2])
pass
编辑:
每次我运行这段代码时,它都会运行但没有错误。我认为它直接转向“通过”。如果我只是使用,一切正常
for i in test_data:
print(i)
但这会打印出不是目标的整个 JSON 对象。有关如何访问这些特定值的任何帮助?
【问题讨论】:
标签: json python-3.x python-requests response