You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从PubMed JSON输出中提取DOI值?

提取PubMed JSON响应中的DOI值

没问题,这完全不是什么浅显的问题——处理嵌套JSON数组的时候,不少人都会卡在这一步!你已经成功定位到了articleids数组,接下来只需要遍历这个数组,找到idtype等于"doi"的元素,然后提取它的value字段就可以了。

具体实现思路和代码示例

首先,先明确你提供的articleids结构:

"articleids": [
    { "idtype": "pubmed", "idtypen": 1, "value": "15674886" },
    { "idtype": "doi", "idtypen": 3, "value": "10.1002/14651858.CD001801.pub2" },
    { "idtype": "rid", "idtypen": 8, "value": "15674886" },
    { "idtype": "eid", "idtypen": 8, "value": "15674886" }
]

假设你已经把PubMed的JSON响应解析成了可操作的数据结构(比如Python字典),下面是两种实用的提取方法:

方法1:循环遍历数组(直观易懂)

# 假设pubmed_data是你解析后的JSON字典,替换成你实际的变量名
target_pmid = "15674886"
article_ids = pubmed_data['result'][target_pmid]['articleids']

doi_value = None
for item in article_ids:
    if item.get('idtype') == 'doi':  # 用get避免键不存在报错
        doi_value = item['value']
        break  # 找到目标后立即终止循环,提升效率

if doi_value:
    print(f"提取到的DOI: {doi_value}")
else:
    print("该文献未收录DOI信息")

方法2:生成器表达式(简洁高效)

target_pmid = "15674886"
article_ids = pubmed_data['result'][target_pmid]['articleids']

# 用next()获取第一个匹配的元素,None作为默认值避免找不到时报错
doi_value = next((item['value'] for item in article_ids if item.get('idtype') == 'doi'), None)

print(doi_value)

关键注意点

  • 部分PubMed文献可能没有DOI信息,所以一定要加入空值判断,防止程序抛出异常
  • 如果你使用其他编程语言(如JavaScript、R),核心逻辑完全一致:遍历数组,匹配idtype为"doi"的元素,提取对应的value

内容的提问来源于stack exchange,提问作者Queen Sarah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:07:41