当前位置 : 主页 > 编程语言 > python >

如何处理json中不带双引号的key的问题

来源:互联网 收集:自由互联 发布时间:2023-01-30
目录 处理json中不带双引号key问题 如下面数据 结果报错 怎么解决呢? json数据key值有引号与没引号的取值 有的json数据的键值是没有引号的 有的返回值的键值是由引号的 第一种json对象
目录
  • 处理json中不带双引号key问题
    • 如下面数据
    • 结果报错
    • 怎么解决呢?
  • json数据key值有引号与没引号的取值
    • 有的json数据的键值是没有引号的
    • 有的返回值的键值是由引号的
    • 第一种json对象取值时
    • 第二种json对象取值时

处理json中不带双引号key问题

在解析网页json数据的时候,我发现python标准库json模块无法加载数据。

如下面数据

import json
s='{symbol:"sh600069",code:"600069",name:"银鸽投资",trade:"3.160",pricechange:"-0.030",changepercent:"-0.940",buy:"3.160",sell:"3.170",settlement:"3.190",open:"3.190",high:"3.210",low:"3.140",volume:3905810,amount:12388386,ticktime:"15:00:00",per:79,pb:2.416,mktcap:513131.494704,nmc:513131.494704,turnoverratio:0.24053}'
js=json.loads(s)

结果报错

提示预期的属性名称应该在双引号里,python无法识别这种不带双引号的json的数据:

怎么解决呢?

第一种方法是使用第三方库demjson.这个安装后直接decode数据源就行。

另一种就是自己写个自定义函数处理下,今天找到一个处理这种问题的自定义函数了。

def jsonfy(s:str)->object:
    #此函数将不带双引号的json的key标准化
    obj = eval(s, type('js', (dict,), dict(__getitem__=lambda s, n: n))())
    return obj

对字json字符串用函数标准化后发现里面的属性名称都带上了引号,而且已经转化为了字典对象了。可以直接进行操作获取数了。

当然正则处理一下也是没问题的。主要就是要排除时间字符串的影响(时间里面刚好带了:也是json的键值分隔符):

json数据key值有引号与没引号的取值

从后台获取数据后我遇到一个bug:(无法获取键值带引号的值)

有的json数据的键值是没有引号的

{pk: "0000c972-ff4c-11e8-aa32-08002782624e", name: "乐陵市云红街道五里田", longitude: 0, latitude: 0, ph: 5.8, …}

有的返回值的键值是由引号的

{"type":"Feature","properties":{"name": "乐陵市云红街道五里田"},"geometry":{"type": "Polygon","coordinates":[[[117.25291089900009,37.74238802900004],[117.25291973100002,37.742300420000049],[117.25345277500003,37.742330535000065],[117.25353190700002,37.742335003000051],[117.25458942300008,37.742394742000045],[117.25606973600009,37.742478319000043],[117.25619680700004,37.74248549400005],[117.25628420100008,37.742490429000043],[117.25640651900005,37.74136234100007],[117.25641165700006,37.741341648000059],[117.25641504000009,37.74131912200005],[117.25653114400006,37.740545954000027],[117.25653071400006,37.740421641000069],[117.25653068100007,37.740412453000033],[117.25653560100011,37.740307598000072],[117.25655447300005,37.740229446000058],[117.25655915900006,37.740201246000026],[117.25659345800011,37.739994860000024],[117.25684111700002,37.740067059000069],[117.2581451420001,37.740602798000054],[117.25818614700006,37.740444250000053],[117.25853526600008,37.739094402000035],[117.25846830700004,37.739081966000072],[117.25785620500005,37.738968281000041],[117.25781732600001,37.738961486000051],[117.25713602600001,37.738842377000026],[117.25686070600011,37.738794245000065],[117.25671882300003,37.738769440000056],[117.2568146970001,37.738190186000054],[117.2569643170001,37.736881095000058],[117.25698481000006,37.736701808000078],[117.25699099500002,37.73664769800007],[117.25699661300007,37.73660231100007],[117.25705518100006,37.736129216000052],[117.25707020300001,37.736007876000031],[117.25708566700007,37.735882976000028],[117.25709686500011,37.735796107000056],[117.25699141300004,37.735788132000039],[117.25638904900006,37.735742576000064],[117.25641200300004,37.735421416000065],[117.25641212300002,37.735419738000076],[117.25636351900005,37.735415323000041],[117.25612242400007,37.735393424000051],[117.25606116100005,37.735677287000044],[117.25561191400004,37.735623850000025],[117.25516524800003,37.735570715000051],[117.25516742700006,37.735611687000073],[117.25516171400011,37.735611452000057],[117.25502474000007,37.735605814000053],[117.25502242800007,37.735605719000034],[117.25428839000006,37.735550486000079],[117.25428113200007,37.735549936000041],[117.25331845700009,37.735476772000027],[117.25323248000007,37.735470238000062],[117.25318180400006,37.735911238000028],[117.25316272300006,37.73599501800004],[117.25316128200006,37.736001351000027],[117.25266654100005,37.735996176000072],[117.25265935100003,37.736096306000036],[117.25262183000007,37.736092756000062],[117.25231804500004,37.736064017000047],[117.25231533300007,37.736063760000036],[117.25213678200009,37.736031242000024],[117.25197550200005,37.736021422000078],[117.25193599600004,37.736019019000025],[117.25133025500008,37.735992714000076],[117.25084230200002,37.735912430000042],[117.25084119200005,37.735918906000052],[117.25081736800007,37.736057886000026],[117.25082260200008,37.736102301000074],[117.25087354300001,37.736126425000066],[117.25093976400001,37.736158596000053],[117.25087864600005,37.73631268400004],[117.25085193300004,37.736415238000063],[117.25081422300002,37.736631421000027],[117.25070216000006,37.73661532300008],[117.25056686900007,37.736595890000046],[117.25056490100008,37.736595607000027],[117.25046782600009,37.736997209000037],[117.25043061400004,37.737151148000066],[117.25043060300004,37.737151193000045],[117.25048086900006,37.73716498400006],[117.2504110110001,37.737501510000072],[117.25036900700002,37.737703829000054],[117.25035327700004,37.737732391000065],[117.25097593100008,37.737956715000053],[117.25096045200007,37.738142150000044],[117.25094070300008,37.73837870300008],[117.25083111100002,37.739691403000052],[117.25080204300002,37.740039503000048],[117.25062461000005,37.742164831000025],[117.25133038800004,37.742210640000053],[117.25234846700005,37.742268158000059],[117.25233010300008,37.742349925000042],[117.25228666500004,37.742543334000061],[117.25221244300008,37.742873835000069],[117.25258687600001,37.742911418000062],[117.25249232700003,37.743441819000054],[117.25307353500011,37.743519689000038],[117.2531786510001,37.742619182000055],[117.25317895600006,37.742616574000067],[117.25288867000006,37.742608579000034],[117.25291089900009,37.74238802900004]]]}}

第一种json对象取值时

可以通过:

console.log(data.data);

的形式取得;

第二种json对象取值时

需要先使用:

var a=eval('(' + data.data.border + ')');

的方式进行转换,然后使用

console.log(a);

的方式取值

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

上一篇:Python学习之字符串常用操作详解
下一篇:没有了
网友评论