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

API JSON解析异常:含转义字符的无效JSON问题求助

解析API返回的无效JSON问题

我尝试从API解析JSON数据,但始终收到包含异常转义字符的无效JSON,返回内容还不完整。

我的代码如下:

import requests
import json

url = "https://547os1.a.searchspring.io/api/search/search.json?ajaxCatalog=v3&resultsFormat=native&siteId=547os1&domain=https%3A%2F%2Fwww.golfbox.com.au%2Fsearch%3Fq%3DCalaway%2BParadym%2BDriver%26Search%3DSEARCH&q=Calaway%20Paradym%20Driver"

response = requests.get(url, headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"}, verify=False)

print(json.loads(response.content))

返回的响应数据存在大量转义斜杠且内容不完整,例如:

/attribute_rule_images/805553_source_1704882887.jpg","cost_price":556.75,"upc":"194518846035","mpn":"4H671241I200","inventory_level":1},{"option_value_id":197674,"value":"12 Degree","option_id":42044,"option":"Loft","id":585853,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-12D-R-HZRD SIL 50","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805553_source_1704882887.jpg","cost_price":556.75,"upc":"194518846035","mpn":"4H671241I200","inventory_level":1},{"option_value_id":197676,"value":"Regular Flex","option_id":42045,"option":"Flex","id":585853,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-12D-R-HZRD SIL 50","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805553_source_1704882887.jpg","cost_price":556.75,"upc":"194518846035","mpn":"4H671241I200","inventory_level":1},{"option_value_id":197680,"value":"Project X HZRDUS Silver 50","option_id":42046,"option":"Shaft","id":585853,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-12D-R-HZRD SIL 50","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805553_source_1704882887.jpg","cost_price":556.75,"upc":"194518846035","mpn":"4H671241I200","inventory_level":1},{"option_value_id":197672,"value":"Mens Right","option_id":42043,"option":"Hand","id":585859,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-9D-S-HZRD SIL 60","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805559_source_1704882887.jpg","cost_price":556.75,"upc":"194518846097","mpn":"4H679041J300","inventory_level":4},{"option_value_id":197675,"value":"9 Degree","option_id":42044,"option":"Loft","id":585859,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-9D-S-HZRD SIL 60","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805559_source_1704882887.jpg","cost_price":556.75,"upc":"194518846097","mpn":"4H679041J300","inventory_level":4},{"option_value_id":197677,"value":"Stiff Flex","option_id":42045,"option":"Flex","id":585859,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-9D-S-HZRD SIL 60","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805559_source_1704882887.jpg","cost_price":556.75,"upc":"194518846097","mpn":"4H679041J300","inventory_level":4},{"option_value_id":197683,"value":"Project X HZRDUS Silver 60","option_id":42046,"option":"Shaft","id":585859,"product_id":41585,"child_sku":"DVR-PARADYM-GRH-9D-S-HZRD SIL 60","price":799.99,"calculated_price":799.99,"retail_price":1069.99,"width":15,"height":15,"depth":125,"purchasing_disabled":false,"image_url":"https://cdn11.bigcommerce.com/s-hiuea1t2mw/product_images/attribute_rule_images/805559_source_1

我已尝试使用response.content、.text、.json()方法处理响应,但问题仍未解决。


解决方法

1. 处理双重编码的JSON

部分API会返回JSON字符串而非直接的JSON对象,需要先解码一次再解析:

import requests
import json

url = "https://547os1.a.searchspring.io/api/search/search.json?ajaxCatalog=v3&resultsFormat=native&siteId=547os1&domain=https%3A%2F%2Fwww.golfbox.com.au%2Fsearch%3Fq%3DCalaway%2BParadym%2BDriver%26Search%3DSEARCH&q=Calaway%20Paradym%20Driver"

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"}
response = requests.get(url, headers=headers, verify=False)

try:
    # 优先尝试直接解析响应
    data = response.json()
    print(data)
except json.JSONDecodeError:
    # 直接解析失败时,先处理响应文本
    raw_text = response.text.strip('()')  # 去除可能的括号包裹
    data = json.loads(raw_text)
    print(data)

2. 清理多余的转义斜杠

如果是URL中的转义斜杠导致解析失败,可以针对性替换:

cleaned_text = response.text.replace('\\/', '/')
data = json.loads(cleaned_text)

3. 确保响应完整接收

内容不完整可能是请求被截断,可强制关闭流式传输并检查响应长度:

response = requests.get(url, headers=headers, verify=False, stream=False)
# 检查响应头中的内容长度是否匹配实际接收长度
expected_length = int(response.headers.get('Content-Length', 0))
if len(response.content) != expected_length:
    print("响应内容不完整,建议重新请求")
else:
    data = json.loads(response.content)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 13:17:02