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

Python字典取值失败及JSON中hits字段含义咨询

问题解答

1. 为什么无法获取firstName对应的值?

因为firstName并不在最外层字典的直接键中,数据嵌套了多层,且存在需要二次解析的JSON字符串:

  • 最外层字典的顶层键是hits,其内部的hits是存储匹配结果的数组
  • 数组第一个元素里的_source.content是JSON格式的字符串,而非可直接访问的字典
  • firstName实际存在于该字符串解析后的basicInformation字段下

修正后的代码如下:

import json 
import requests

# Person's ID
id = 1194452

# Url
info = requests.get(f'https://api.brokercheck.finra.org/search/individual/{id}?hl=true&includePrevious=true&sort=bc_lastname_sort+asc,bc_firstname_sort+asc,bc_middlename_sort+asc,score+desc&wt=json') 

# Convert to JSON
x = info.json()

# 逐层解析嵌套结构
hit = x['hits']['hits'][0]
content_str = hit['_source']['content']
content_dict = json.loads(content_str)

# 获取目标值
print(content_dict['basicInformation']['firstName'])

2. 返回JSON中的hits是什么?

你搜索到的「Hyperlink Induced Topic Search」和这里的hits完全无关。这里的hits是Elasticsearch风格的搜索结果字段:

  • 外层hits是搜索结果的容器,包含total(匹配结果总数)和内层hits(具体匹配文档数组)两个核心字段
  • 内层hits数组中的每个元素,都是一条匹配到的完整数据记录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 05:35:41