在Python3中解析JSON数据时遇到问题
需要帮助从中获取分数值吗在Python3中解析JSON数据时遇到问题,python,json,Python,Json,需要帮助从中获取分数值吗 { "name": "Apdex", "timeslices": [ { "from": "2020-01-15T19:49:00+00:00", "to": "2020-01-15T19:50:00+00:00", "values": { "score": 0.94, "s": 136000,
{
"name": "Apdex",
"timeslices": [
{
"from": "2020-01-15T19:49:00+00:00",
"to": "2020-01-15T19:50:00+00:00",
"values": {
"score": 0.94,
"s": 136000,
"t": 9440,
"f": 4510,
"count": 150107,
"value": 0.94,
"threshold": 0.5,
"threshold_min": 0.5
}
}
}
这是我从对的API调用中收到的JSON
我在输出正确的分数时遇到问题。任何帮助都将不胜感激。假设您的输入是变量
input
json
import json
data = json.loads(input)
result = data['metric_data']['metrics']
这将为您提供一个列表,如果您想要问题中的确切列表,您可以在列表中进行筛选:
final_result = [score for score in result if score['name'] == 'Apdex']
slices = final_result[0]['timeslices']
values = [slice['values']['score'] for slice in slices]
你能发布你不想做的代码和它的输出吗?你能澄清你提供的JSON中“正确”的分数是多少吗?还有,你试过的代码也很有用。小家伙,正确的分数是0.94分,这不是我想要的。它正在返回字符串:[{'name':'Apdex','timeslices':[{'from':'2020-01-15T21:20:00+00:00','to':'2020-01-15T21:21:00+00:00','values':{'score':0.94,'s':52900,'t':4460,'f':1270,'count':58631,'value':0.94,'threshold':0.5,'threshold\u min':0.059}]。我希望返回的是分数中的0.94值。这也给出了一个错误:TypeError:列表索引必须是整数或切片,而不是标准。这接近我需要的,但是它给了我两个分数“分数”:0.94是我需要的,但它也打印了“分数”:1.0,我不想要。
final_result = [score for score in result if score['name'] == 'Apdex']
slices = final_result[0]['timeslices']
values = [slice['values']['score'] for slice in slices]
d = {
"metric_data": {
"from": "2020-01-15T19:51:32+00:00",
"to": "2020-01-15T19:53:02+00:00",
"metrics_not_found": [],
"metrics_found": [
"Apdex",
"EndUser/Apdex"
],
"metrics": [
{
"name": "Apdex",
"timeslices": [
{
"from": "2020-01-15T19:49:00+00:00",
"to": "2020-01-15T19:50:00+00:00",
"values": {
"score": 0.94,
"s": 136000,
"t": 9440,
"f": 4510,
"count": 150107,
"value": 0.94,
"threshold": 0.5,
"threshold_min": 0.5
}
}
]
},
{
"name": "EndUser/Apdex",
"timeslices": [
{
"from": "2020-01-15T19:49:00+00:00",
"to": "2020-01-15T19:50:00+00:00",
"values": {
"score": 1.0,
"s": 0,
"t": 0,
"f": 0,
"count": 0,
"value": None,
"threshold": 7.0,
"threshold_min": 7.0
}
}
]
}
]
}
}
for m in d['metric_data']['metrics']:
print(m['timeslices'][0]['values']['score'])