有什么方法可以将变形的json加载到python对象中?

时间:2019-09-25 05:47:49

标签: json python-3.x

点击API后,我得到了json数据。 当我尝试使用json.loads(response.text)将json加载到python中时,出现定界符错误。

当检查json中的几个字段不带“,”时将其分隔。

{
"id":"142379",
"label":"1182_Mailer_PROD",
"location":"Bangalore, India",
"targetType":"HTTPS performance",
"frequency":"15",
"fails":"2764",
"totalUptime":"85.32"
"tests":[
{"date":"09-24-2019 09:31","status":"Could not resolve: mailer.accenture.com (DNS server returned answer with no data)","responseTime":"0.000","dnsTime":"0.000","connectTime":"0.000","redirectTime":"0.000","firstbyteTime":"0.000","lastbyteTime":"0.000","pingLoss":"0.00","pingMin":"0.000","pingAvg":"0.000","pingMax":"0.000","size":"0","md5hash":"(null)"}
]
}
,
{
"id":"158651",
"label":"11883-GR dd-WSP",
"location":"Chicago, IL",
"targetType":"Performance transaction",
"frequency":"15",
"fails":"5919",
"totalUptime":"35.14"
,"tests":[
{"date":"09-24-2019 09:26","status":"Keywords not found - Working","responseTime":"0.669","stepresults":[
{"stepid":"1","date":"09-24-2019 09:26","status":"OK","responseTime":"0.453","dnsTime":"0.000","connectTime":"0.025","redirectTime":"0.264","firstbyteTime":"0.141","lastbyteTime":"0.024","size":"22351","md5hash":"ca002cf662980511a9faa88286f2ee96"},
{"stepid":"2","date":"09-24-2019 09:26","status":"Keywords not found - Working","responseTime":"0.216","dnsTime":"0.000","connectTime":"0.023","redirectTime":"0.000","firstbyteTime":"0.171","lastbyteTime":"0.022","size":"22457","md5hash":"38327404e4f2392979aa7dfa27118f4e"}
]}]
}

这是响应中的一小部分数据,您可能会看到“ totalUptime”:“ 85.32”没有逗号分隔。

您能否让我知道即使json变形也如何将数据加载到python对象中

1 个答案:

答案 0 :(得分:0)

变形的JSON不是JSON,因此显然您无法使用标准过程加载它。加载它只有两种可能性:

  • 创建自己的解析器
  • 修改输入以符合JSON标准

两种可能性都需要您定义要导入的格式。如果格式没有逗号是可以的,那么您必须定义分隔符。

在您发布的示例中,很难对如何定义输入格式进行任何确定的评估。因此,您可能必须编写一个基本的解析器,并通过尝试和错误将其近似为要解析的输入。