鱼C论坛

 找回密码
 立即注册
查看: 2255|回复: 2

[已解决]json加载错误问题,请大神帮忙看下,谢谢

[复制链接]
发表于 2022-12-18 19:59:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 futui 于 2022-12-18 20:08 编辑

下面数据是从深圳证券交易所所下载:【.szse.cn/market/stock/indicator/index.html】
response = '[{"metadata":{"catalogid":"1803_after","name":"深圳市场","excel":"xlsx","pagetype":"tabs","subname":"","tabkey":"tab1","csskey":[],"pagesize":-1,"pageno":0,"pagecount":0,"recordcount":0,"showrecordcount":false,"header":"","footer":"<table>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td class=\"link1\"><font color='black'>深圳市场、主板、创业板数据统计范围包含存托凭证。</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>","reporttype":null,"tabcss":null,"conditions":[{"label":"查询日期","name":"txtQueryDate","hidden":"0","defaultValue":"2022-12-16","labelstyle":"1","required":"true","inputType":"date","typeAhead":"","options":null,"property":null,"maxlength":null,"otherProperties":null,"clientvalidate":""}],"colStyle":{"zbmc":{"width":"20","align":"left","group":"false","hidden":"0"},"brsz":{"width":"17","align":"right","group":"false","hidden":"0"},"bsrzj":{"width":"17","align":"right","group":"false","hidden":"0"},"fd":{"width":"12","align":"right","group":"false","hidden":"0"}},"cols":{"zbmc":"指标名称","brsz":"本日数值","bsrzj":"比上日增减","fd":"幅度%"},"colspan":0,"notes":null,"topHeader":"","hidden":"0","refreshCondition":false},"data":[],"error":null},{"metadata":{"catalogid":"1803_after","name":"主板","excel":"xlsx","pagetype":"tabs","subname":"2022-12-16","tabkey":"tab2","csskey":[],"pagesize":-1,"pageno":0,"pagecount":0,"recordcount":0,"showrecordcount":false,"header":"","footer":"<table>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td class=\"link1\"><font color='black'>深圳市场、主板、创业板数据统计范围包含存托凭证。</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>","reporttype":null,"tabcss":null,"conditions":[{"label":"查询日期","name":"txtQueryDate","hidden":"0","defaultValue":"2022-12-16","labelstyle":"1","required":"true","inputType":"date","typeAhead":"","options":null,"property":null,"maxlength":null,"otherProperties":null,"clientvalidate":""}],"colStyle":{"indicator":{"align":"left","group":"false","hidden":"0"},"today":{"align":"right","group":"false","hidden":"0"},"increase":{"align":"right","group":"false","hidden":"0"},"fd":{"align":"right","group":"false","hidden":"0"}},"cols":{"indicator":"指标名称","today":"本日数值","increase":"比上日增减","fd":"幅度%"},"colspan":0,"notes":null,"topHeader":"","hidden":"0","refreshCondition":false},"data":[{"indicator":"新上市公司数","today":"0","increase":"","fd":""},{"indicator":"上市公司数","today":"1,510","increase":"0","fd":"0.00"},{"indicator":"总股本(亿)","today":"19,883.51","increase":"0.09","fd":"0.00"},{"indicator":"流通股本(亿)","today":"17,530.72","increase":"1.26","fd":"0.00"},{"indicator":"总市值(亿元)","today":"218,156.21","increase":"-1,010.61","fd":"-0.46"},{"indicator":"流通市值(亿元)","today":"190,831.54","increase":"-810.36","fd":"-0.42"},{"indicator":"成交金额(亿元)","today":"3,021.99","increase":"-98.36","fd":"-3.15"},{"indicator":"成交量(亿)","today":"309.41","increase":"3.50","fd":"1.14"},{"indicator":"成交笔数(万笔)","today":"2,740.88","increase":"13.35","fd":"0.48"},{"indicator":"平均市盈率(倍)","today":"20.15","increase":"-0.09","fd":"-0.44"}],"error":null},{"metadata":{"catalogid":"1803_after","name":"创业板","excel":"xlsx","pagetype":"tabs","subname":"","tabkey":"tab4","csskey":[],"pagesize":-1,"pageno":0,"pagecount":0,"recordcount":0,"showrecordcount":false,"header":"","footer":"<table>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td class=\"link1\"><font color='black'>深圳市场、主板、创业板数据统计范围包含存托凭证。</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>","reporttype":null,"tabcss":null,"conditions":[{"label":"查询日期","name":"txtQueryDate","hidden":"0","defaultValue":"2022-12-16","labelstyle":"1","required":"true","inputType":"date","typeAhead":"","options":null,"property":null,"maxlength":null,"otherProperties":null,"clientvalidate":""}],"colStyle":{"indicator":{"align":"left","group":"false","hidden":"0"},"today":{"align":"right","group":"false","hidden":"0"},"increase":{"align":"right","group":"false","hidden":"0"},"fd":{"align":"right","group":"false","hidden":"0"}},"cols":{"indicator":"指标名称","today":"本日数值","increase":"比上日增减","fd":"幅度%"},"colspan":0,"notes":null,"topHeader":"","hidden":"0","refreshCondition":false},"data":[],"error":null}]'

我想提取第二段主板的数据,用下面2句对它上面字符串进行解析时,出现错误提示:“json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 3991 (char 3990)”,我要怎么才能提出到主板的数据(数据框),请大神帮帮忙,谢谢!

json_str = response[response.index('[') : response.rindex(']')]
data = json.loads(json_str)['metadata'][1]['data']
最佳答案
2022-12-18 23:11:51
多次一举。。 它API本来就返回的是 JSON 格式的数据,直接用 response对象的 .json() 就可以转为 字典数据。
import requests 
url = 'http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1803_after&loading=first&random=0.5637480142829792'
res = requests.get(url)
print(res.json())   # 或者 json.loads(res.text)

ps:大多数接口数据返回的都是 json 。有一部分会加个 xxxxjQueryxxx({...JSON_str....})

ps2: 不知道你上面的数据是怎么回事。。按理说 对于数组类型的字符串数据也是可以反序列化的, 比如 json.loads('[{"a":[1,2,3]}, {"b":[4,5,6]}]') 这是可以的。



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-18 23:11:51 | 显示全部楼层    本楼为最佳答案   
多次一举。。 它API本来就返回的是 JSON 格式的数据,直接用 response对象的 .json() 就可以转为 字典数据。
import requests 
url = 'http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1803_after&loading=first&random=0.5637480142829792'
res = requests.get(url)
print(res.json())   # 或者 json.loads(res.text)

ps:大多数接口数据返回的都是 json 。有一部分会加个 xxxxjQueryxxx({...JSON_str....})

ps2: 不知道你上面的数据是怎么回事。。按理说 对于数组类型的字符串数据也是可以反序列化的, 比如 json.loads('[{"a":[1,2,3]}, {"b":[4,5,6]}]') 这是可以的。



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-12-18 23:17:01 | 显示全部楼层
json_str = re.findall(r'\"data\":(.*?),\"error',response)[1],我用它提出数据就能装载了,谢谢大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 07:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表