所以我有以下JSON块:
b'{"data":[{"categories":[{"id":"IAB3","label":"Business","parent":"IAB3","score":"0.223819028028717559","confident":true}],"url":"megatel.de"}]}'
我只需要一个包含第一个标签字段内容的字符串。
答案 0 :(得分:2)
以下代码段将输出返回为Business
import json
data = json.loads('{"data":[{"categories":[{"id":"IAB3","label":"Business","parent":"IAB3","score":"0.223819028028717559","confident":true}],"url":"megatel.de"}]}')
print(data['data'][0]['categories'][0]['label'])
如果你有更多这样的数据,你可以迭代data
变量并通过在两个地方用迭代器替换0
索引来获得所需的结果。
例如,如果json有点如下
{"data":
[
{"categories":
[
{"id":"IAB3",
"label":"Business",
"parent":"IAB3",
"score":"0.223819028028717559",
"confident":true}
],
"url":"megatel.de"
},
{"categories":
[
{"id":"IAB3",
"label":"Business",
"parent":"IAB3",
"score":"0.223819028028717559",
"confident":true}
],
"url":"megatel.de"
}
]
}
您可以使用以下脚本来获得类似的输出。
for entry in data['data']:
for categories in entry['categories']:
print categories['label']
答案 1 :(得分:0)
x = b'{"data":[{"categories":[{"id":"IAB3","label":"Business","parent":"IAB3","score":"0.223819028028717559","confident":true}],"url":"megatel.de"}]}'
(x.split('"label":')[1]).split(",")[0][1:-1]
>>'Business'
答案 2 :(得分:0)
如果您也可以尝试使用正则表达式方法:
td