首页 > 解决方案 > 在python语言中将类似json的结构转换为json

问题描述

我正在调用以文本/html 格式发送响应的第 3 方 API。它是一个键值对,但不完全是 json。

当前:{ data:"true" ,results:15} 预期:"data":"true" , "results":15}

我正在尝试解析使用json.loads来创建字典,但从结构中可以清楚地看出,它并不完全是 json,python 也会引发错误。

{JSONDecodeError}期望用双引号括起来的属性名称:第 1 行第 3 列(字符 2)

有没有办法修复响应并将其转换为正确的 json,或者有没有其他方法可以从响应中获取键值对

在这种情况下,yaml 无法帮助我,因为整个数据:

{ success:true ,results:9,rows:[{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.5/- Per Share",exDt:"02-Sep-2010",recordDt:"-",bcStartDt:"04-Sep-2010",bcEndDt:"13-Sep-2010",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.8.50 Per Share",exDt:"23-Aug-2011",recordDt:"-",bcStartDt:"25-Aug-2011",bcEndDt:"03-Sep-2011",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.9/- Per Share",exDt:"20-Jul-2012",recordDt:"-",bcStartDt:"24-Jul-2012",bcEndDt:"02-Aug-2012",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting/Dividend Rs 9/- Per Share",exDt:"09-Jul-2013",recordDt:"-",bcStartDt:"11-Jul-2013",bcEndDt:"20-Jul-2013",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting / Dividend - Rs 2/- Per Share",exDt:"04-Jul-2014",recordDt:"-",bcStartDt:"08-Jul-2014",bcEndDt:"17-Jul-2014",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting / Dividend - Rs 2.20/- Per Share",exDt:"28-May-2015",recordDt:"-",bcStartDt:"01-Jun-2015",bcEndDt:"05-Jun-2015",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/ Dividend -Re 0.70 Per Share",exDt:"29-Jul-2016",recordDt:"-",bcStartDt:"02-Aug-2016",bcEndDt:"11-Aug-2016",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/Dividend - Re 0.90 Per Share",exDt:"03-Jul-2017",recordDt:"-",bcStartDt:"05-Jul-2017",bcEndDt:"14-Jul-2017",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/Dividend- Re 1 Per Share",exDt:"02-Aug-2018",recordDt:"-",bcStartDt:"04-Aug-2018",bcEndDt:"10-Aug-2018",ndStartDt:"-",comp:"The Federal Bank  Limited",isin:"INE171A01011",ndEndDt:"-"}]}

使用yaml后变成:

{'success:true': None, 'results:9': None, 'rows': [{'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.5/- Per Share"': None, 'exDt:"02-Sep-2010"': None, 'recordDt:"-"': None, 'bcStartDt:"04-Sep-2010"': None, 'bcEndDt:"13-Sep-2010"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.8.50 Per Share"': None, 'exDt:"23-Aug-2011"': None, 'recordDt:"-"': None, 'bcStartDt:"25-Aug-2011"': None, 'bcEndDt:"03-Sep-2011"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.9/- Per Share"': None, 'exDt:"20-Jul-2012"': None, 'recordDt:"-"': None, 'bcStartDt:"24-Jul-2012"': None, 'bcEndDt:"02-Aug-2012"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting/Dividend Rs 9/- Per Share"': None, 'exDt:"09-Jul-2013"': None, 'recordDt:"-"': None, 'bcStartDt:"11-Jul-2013"': None, 'bcEndDt:"20-Jul-2013"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting / Dividend - Rs 2/- Per Share"': None, 'exDt:"04-Jul-2014"': None, 'recordDt:"-"': None, 'bcStartDt:"08-Jul-2014"': None, 'bcEndDt:"17-Jul-2014"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting / Dividend - Rs 2.20/- Per Share"': None, 'exDt:"28-May-2015"': None, 'recordDt:"-"': None, 'bcStartDt:"01-Jun-2015"': None, 'bcEndDt:"05-Jun-2015"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/ Dividend -Re 0.70 Per Share"': None, 'exDt:"29-Jul-2016"': None, 'recordDt:"-"': None, 'bcStartDt:"02-Aug-2016"': None, 'bcEndDt:"11-Aug-2016"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/Dividend - Re 0.90 Per Share"': None, 'exDt:"03-Jul-2017"': None, 'recordDt:"-"': None, 'bcStartDt:"05-Jul-2017"': None, 'bcEndDt:"14-Jul-2017"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/Dividend- Re 1 Per Share"': None, 'exDt:"02-Aug-2018"': None, 'recordDt:"-"': None, 'bcStartDt:"04-Aug-2018"': None, 'bcEndDt:"10-Aug-2018"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank  Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}]}

我无法访问:

yaml.load(corporate_action)['rows'][0]['sym']

标签: pythonjsonpython-3.xkey-value

解决方案


正则表达式可能会有所帮助。您可以使用re.sub

前任:

import re
import json


data = '{ data:"true" ,results:15}'
data = json.loads(re.sub(r"(\w+):", r'"\1":', data))
print(data)

输出:

{u'data': u'true', u'results': 15}

推荐阅读