1. 程式人生 > >json資料key值有引號與沒引號的取值

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);

的方式取值