如何访问 json 中的嵌套值
How to access nested values in json
我正在尝试使用 api 和 python 从 scopus 获取数据。我使用 python 模块请求进行查询。
查询的响应为我提供了一个 json,其值如下所示。
{
"search-results":{
"opensearch:totalResults": "1186741",
"opensearch:startIndex": "0",
"opensearch:itemsPerPage": "25",
"opensearch:Query":{
"@role": "request",
"@searchTerms": "all(machine learning)",
"@startPage": "0"
},
"link":[
{
"@_fa": "true",
"@ref": "self",
"@href": "api query",
“@类型”:"application/json"
},
{
"@_fa": "true",
"@ref": "first",
"@href": "api query",
“@类型”:"application/json"
},
{
"@_fa": "true",
"@ref": "next",
"@href": "api query",
“@类型”:"application/json"
},
{
"@_fa": "true",
"@ref": "last",
"@href": "api query",
“@类型”:"application/json"
}
],
"entry": [
{
"@_fa": "true",
"link":[
{
"@_fa": "true",
"@ref": "self",
"@href": "https://api.elsevier.com/content/abstract/scopus_id/85081889595"
},
{
"@_fa": "true",
“@ref”:"author-affiliation",
"@href": "https://api.elsevier.com/content/abstract/scopus_id/85081889595?field=author,affiliation"
},
{
"@_fa": "true",
“@ref”:"scopus",
"@href": "https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=85081889595&origin=inward"
},
{
"@_fa": "true",
“@ref”:"scopus-citedby",
"@href": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=85081889595&origin=inward"
}
],
"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081889595",
"dc:identifier": "SCOPUS_ID:85081889595",
"eid": "2-s2.0-85081889595",
"dc:title": "Recognizing hotspots in Brief Eclectic Psychotherapy for PTSD by text and audio mining",
"dc:creator": "Wiegersma S.",
"prism:publicationName": "European Journal of Psychotraumatology",
"prism:issn": "20008198",
"prism:eIssn": "20008066",
"prism:volume": "11",
"prism:issueIdentifier": "1",
"prism:pageRange":空,
"prism:coverDate": "2020-12-31",
"prism:coverDisplayDate": "2020 年 12 月 31 日",
"prism:doi": "10.1080/20008198.2020.1726672",
"citedby-count": "0",
"affiliation": [
{
"@_fa": "true",
"affilname": "University of Twente",
"affiliation-city": "Enschede",
"affiliation-country": "Netherlands"
}
],
"prism:aggregationType": "Journal",
"subtype": "ar",
"subtypeDescription": "Article",
"article-number": "1726672",
"source-id": "21100394256",
"openaccess": "1",
"openaccessFlag": 是的
},
但是,响应是嵌套的 json,我无法访问它的内部元素,例如键 dc:creator、citedby-count 等
任何人都可以帮助我如何访问它的所有部分,例如作者姓名、引用者、隶属关系等。
我想将此结果存储为 csv,我可以将其用于进一步的操作。
直接应用
df = pandas.read_json(文件名)
没有产生正确的结果格式:我得到这样的 table。
entry [{'@_fa': 'true', 'link': [{'@_fa': 'true', '@...
link [{'@_fa': 'true', '@ref': 'self', '@href': 'ht...
opensearch:Query {'@role': 'request', '@searchTerms': 'all(mach...
opensearch:itemsPerPage 25
opensearch:startIndex 0
opensearch:totalResults 1186741
我也试过通过嵌套字典访问列表到字典的方法,但在某些时候,我卡住了。
with open('data.json', encoding='utf-8') as access:
read_file = json.load(access)
…
type(read_file)
这是一个字典,所以我按照字典的语法进一步访问,它会在某个时候转换为列表,然后再次转换为字典。
我的主要要求是 - **如何创建一个包含 headers 列的 csv 文件,它是条目标签内的标签,例如 dc:identifier、dc:title、dc:creator , citedby-count 等,以及其中的值 **
在此处输入代码
import json
dict_data = json.loads(response)
print(dict_data['key'])
你是这个意思吗?
我正在尝试使用 api 和 python 从 scopus 获取数据。我使用 python 模块请求进行查询。 查询的响应为我提供了一个 json,其值如下所示。
{ "search-results":{ "opensearch:totalResults": "1186741", "opensearch:startIndex": "0", "opensearch:itemsPerPage": "25", "opensearch:Query":{ "@role": "request", "@searchTerms": "all(machine learning)", "@startPage": "0" }, "link":[ { "@_fa": "true", "@ref": "self", "@href": "api query", “@类型”:"application/json" }, { "@_fa": "true", "@ref": "first", "@href": "api query", “@类型”:"application/json" }, { "@_fa": "true", "@ref": "next", "@href": "api query", “@类型”:"application/json" }, { "@_fa": "true", "@ref": "last", "@href": "api query", “@类型”:"application/json" } ], "entry": [ { "@_fa": "true", "link":[ { "@_fa": "true", "@ref": "self", "@href": "https://api.elsevier.com/content/abstract/scopus_id/85081889595" }, { "@_fa": "true", “@ref”:"author-affiliation", "@href": "https://api.elsevier.com/content/abstract/scopus_id/85081889595?field=author,affiliation" }, { "@_fa": "true", “@ref”:"scopus", "@href": "https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=85081889595&origin=inward" }, { "@_fa": "true", “@ref”:"scopus-citedby", "@href": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=85081889595&origin=inward" } ], "prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081889595", "dc:identifier": "SCOPUS_ID:85081889595", "eid": "2-s2.0-85081889595", "dc:title": "Recognizing hotspots in Brief Eclectic Psychotherapy for PTSD by text and audio mining", "dc:creator": "Wiegersma S.", "prism:publicationName": "European Journal of Psychotraumatology", "prism:issn": "20008198", "prism:eIssn": "20008066", "prism:volume": "11", "prism:issueIdentifier": "1", "prism:pageRange":空, "prism:coverDate": "2020-12-31", "prism:coverDisplayDate": "2020 年 12 月 31 日", "prism:doi": "10.1080/20008198.2020.1726672", "citedby-count": "0", "affiliation": [ { "@_fa": "true", "affilname": "University of Twente", "affiliation-city": "Enschede", "affiliation-country": "Netherlands" } ], "prism:aggregationType": "Journal", "subtype": "ar", "subtypeDescription": "Article", "article-number": "1726672", "source-id": "21100394256", "openaccess": "1", "openaccessFlag": 是的 },
但是,响应是嵌套的 json,我无法访问它的内部元素,例如键 dc:creator、citedby-count 等
任何人都可以帮助我如何访问它的所有部分,例如作者姓名、引用者、隶属关系等。 我想将此结果存储为 csv,我可以将其用于进一步的操作。
直接应用
df = pandas.read_json(文件名)
没有产生正确的结果格式:我得到这样的 table。
entry [{'@_fa': 'true', 'link': [{'@_fa': 'true', '@...
link [{'@_fa': 'true', '@ref': 'self', '@href': 'ht...
opensearch:Query {'@role': 'request', '@searchTerms': 'all(mach...
opensearch:itemsPerPage 25
opensearch:startIndex 0
opensearch:totalResults 1186741
我也试过通过嵌套字典访问列表到字典的方法,但在某些时候,我卡住了。
with open('data.json', encoding='utf-8') as access:
read_file = json.load(access)
…
type(read_file)
这是一个字典,所以我按照字典的语法进一步访问,它会在某个时候转换为列表,然后再次转换为字典。
我的主要要求是 - **如何创建一个包含 headers 列的 csv 文件,它是条目标签内的标签,例如 dc:identifier、dc:title、dc:creator , citedby-count 等,以及其中的值 ** 在此处输入代码
import json
dict_data = json.loads(response)
print(dict_data['key'])
你是这个意思吗?