【发布时间】:2020-09-11 16:49:27
【问题描述】:
我正在尝试获取 JSON 中嵌入键的值,但我不断收到错误消息:TypeError: string indices must be integers。
JSON 是:
{"id": "example", "metadata": {"operation": "retrieve", "provider": "Oxford University Press", "schema": "RetrieveEntry"}, "results": [{"id": "example", "language": "en-gb", "lexicalEntries": [{"entries": [{"etymologies": ["late Middle English: from Old French, from Latin exemplum, from eximere \u2018take out\u2019, from ex- \u2018out\u2019 + emere \u2018take\u2019. Compare with sample"], "pronunciations": [{"audioFile": "https://audio.oxforddictionaries.com/en/mp3/example_gb_1.mp3", "dialects": ["British English"], "phoneticNotation": "IPA", "phoneticSpelling": "\u026a\u0261\u02c8z\u0251\u02d0mp(\u0259)l"}, {"dialects": ["British English"], "phoneticNotation": "IPA", "phoneticSpelling": "\u025b\u0261\u02c8z\u0251\u02d0mp(\u0259)l"}], "senses": [{"definitions": ["a thing characteristic of its kind or illustrating a general rule"], "examples": [{"text": "advertising provides a good example of an industry where dreams have faded"}], "id": "m_en_gbus0339130.007", "semanticClasses": [{"id": "example", "text": "Example"}], "shortDefinitions": ["thing characteristic of its kind"], "subsenses": [{"definitions": ["a written problem or exercise designed to illustrate a rule"], "examples": [{"text": "a workbook and a data set will enable the researcher to follow worked examples"}], "id": "m_en_gbus0339130.012", "semanticClasses": [{"id": "mathematical_expression", "text": "Mathematical_Expression"}], "shortDefinitions": ["problem or exercise illustrating rule"]}], "synonyms": [{"language": "en", "text": "specimen"}, {"language": "en", "text": "sample"}, {"language": "en", "text": "exemplar"}, {"language": "en", "text": "exemplification"}, {"language": "en", "text": "instance"}, {"language": "en", "text": "case"}, {"language": "en", "text": "representative case"}, {"language": "en", "text": "typical case"}, {"language": "en", "text": "case in point"}, {"language": "en", "text": "illustration"}], "thesaurusLinks": [{"entry_id": "example", "sense_id": "t_en_gb0005163.001"}]}, {"definitions": ["a person or thing regarded in terms of their fitness to be imitated"], "examples": [{"text": "it is important that parents should set an example"}, {"text": "he followed his brother's example and deserted his family"}], "id": "m_en_gbus0339130.014", "semanticClasses": [{"id": "example", "text": "Example"}], "shortDefinitions": ["person or thing imitated"], "synonyms": [{"language": "en", "text": "precedent"}, {"language": "en", "text": "lead"}, {"language": "en", "text": "guide"}, {"language": "en", "text": "model"}, {"language": "en", "text": "pattern"}, {"language": "en", "text": "blueprint"}, {"language": "en", "text": "template"}, {"language": "en", "text": "paradigm"}, {"language": "en", "text": "exemplar"}, {"language": "en", "text": "ideal"}, {"language": "en", "text": "standard"}], "thesaurusLinks": [{"entry_id": "example", "sense_id": "t_en_gb0005163.002"}]}]}], "language": "en-gb", "lexicalCategory": {"id": "noun", "text": "Noun"}, "phrases": [{"id": "for_example", "text": "for example"}, {"id": "make_an_example_of", "text": "make an example of"}], "text": "example"}, {"entries": [{"notes": [{"text": "\"be exampled\"", "type": "wordFormNote"}], "pronunciations": [{"audioFile": "https://audio.oxforddictionaries.com/en/mp3/example_gb_1.mp3", "dialects": ["British English"], "phoneticNotation": "IPA", "phoneticSpelling": "\u026a\u0261\u02c8z\u0251\u02d0mp(\u0259)l"}, {"dialects": ["British English"], "phoneticNotation": "IPA", "phoneticSpelling": "\u025b\u0261\u02c8z\u0251\u02d0mp(\u0259)l"}], "senses": [{"definitions": ["be illustrated or exemplified"], "examples": [{"text": "the extent of Allied naval support is exampled by the navigational specialists provided"}], "id": "m_en_gbus0339130.016", "shortDefinitions": ["be illustrated or exemplified"]}]}], "language": "en-gb", "lexicalCategory": {"id": "verb", "text": "Verb"}, "phrases": [{"id": "for_example", "text": "for example"}, {"id": "make_an_example_of", "text": "make an example of"}], "text": "example"}], "type": "headword", "word": "example"}], "word": "example"}
我想要的值在"results" > "lexicalEntries" > "entries" > "senses" > "definitions"找到。
如何使用 Python 从该键中获取值?
代码:
import requests
import json
import urllib.request
app_id = "XXXXXX"
app_key = "XXXXXXXXXXXXXXXXXXXXXXX"
language = "en-gb"
word_id = "example"
url1 = "https://od-api.oxforddictionaries.com:443/api/v2/entries/" + language + "/" + word_id.lower()
r1 = urllib.request.Request(url1, headers={"app_id": app_id, "app_key": app_key})
with urllib.request.urlopen(r1) as url:
data = json.loads(url.read().decode())
data = json.dumps(data)
#print(data)
#print(data['results']['lexicalEntries']['entries']['senses']['definitions'])
print(data['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions'])
【问题讨论】:
-
你试过在 python 中做这个吗?你能发布一些代码吗?您也可以使用 json 路径查询来实现您想要的,python 中有一些库可以实现这一点
-
@FabioBohnenberger pastebin.com/UzkYF1MB
-
你为什么在
json.loads之后使用json.dumps? -
它作为字典加载,以便将其转换为 JSON
-
调用
dumps()会将数据转换为字符串,因此您不再需要字典。把它拿出来。
标签: python json python-3.x