2017-02-18 33 views
-3

我有一個如下所示的代碼,它是域上的JSON代碼。 {"userCount":64,"users":[{"id":"553","acid":"553","ac":"240","cs":"Le AlphaDelta","st":{"gr":false},"co":[26.93526727771073,-79.9251579421954,10667.999977505215,-163.9999997562341,-2.2288855660652414,2.4376547336953407e-7],"ve":[-0.000002161617968514662,-6.915441330619387e-7,0.000004838653476326726,0,-2.27708260122718e-10,0],"ti":1487455628374.1194,"aircraft":"240","callsign":"Le AlphaDelta","time":1487455628374.1194} 我想python指向這個,並從它採取的數據,如我想採取CS,AC等我怎麼去做這件事?Python請求 - 以JSON數據並在python中使用它

編輯: 我想獲取數據,比如json文件中第一個人的id,因爲文件中會有多個用戶。所以我不能擁有我在這裏的數據。基本上,我想採取上面的數據,並把它放在python中,並分解它,所以在python id [1] =第一個用戶的ID。

+1

歡迎堆棧溢出。請參閱[問]並提供[mcve]。 –

回答

0

看一看https://docs.python.org/2.7/library/json.html

import json 
j = json.loads('{"userCount":64,"users":[{"id":"553","acid":"553","ac":"240","cs":"Le AlphaDelta","st":{"gr":false},"co":[26.93526727771073,-79.9251579421954,10667.999977505215,-163.9999997562341,-2.2288855660652414,2.4376547336953407e-7],"ve":[-0.000002161617968514662,-6.915441330619387e-7,0.000004838653476326726,0,-2.27708260122718e-10,0],"ti":1487455628374.1194,"aircraft":"240","callsign":"Le AlphaDelta","time":1487455628374.1194}') 
print j['ac'] 
+0

如果我想從ac中獲取數據而沒有id等,該怎麼辦? –

+0

你可以這樣做:'爲我在j ['用戶']:打印我['ac']'所有交流應該打印 – clue404

+0

我得到這個錯誤'追蹤(最近呼叫最後): 文件「C:\ Users \ Kyle \ AppData \ Local \ Programs \ Python \ Python36 \ yah.py」,第4行, for i [j'users']: TypeError:'Response'object is not subscriptable' –

0

你的JSON文本似乎並沒有被很好地形成:

>>> import json 
>>> json.loads('{"userCount":64,"users":[{"id":"553","acid":"553","ac":"240","cs":"Le AlphaDelta","st":{"gr":false},"co":[26.93526727771073,-79.9251579421954,10667.999977505215,-163.9999997562341,-2.2288855660652414,2.4376547336953407e-7],"ve":[-0.000002161617968514662,-6.915441330619387e-7,0.000004838653476326726,0,-2.27708260122718e-10,0],"ti":1487455628374.1194,"aircraft":"240","callsign":"Le AlphaDelta","time":1487455628374.1194}') 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python3.5/json/__init__.py", line 319, in loads 
    return _default_decoder.decode(s) 
    File "/usr/lib/python3.5/json/decoder.py", line 339, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/usr/lib/python3.5/json/decoder.py", line 355, in raw_decode 
    obj, end = self.scan_once(s, idx) 
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 422 (char 421) 

明白了:

>>> import json 
>>> j = json.loads('{"userCount":64,"users":[{"id":"553","acid":"553","ac":"240","cs":"Le AlphaDelta","st":{"gr":false},"co":[26.93526727771073,-79.9251579421954,10667.999977505215,-163.9999997562341,-2.2288855660652414,2.4376547336953407e-7],"ve":[-0.000002161617968514662,-6.915441330619387e-7,0.000004838653476326726,0,-2.27708260122718e-10,0],"ti":1487455628374.1194,"aircraft":"240","callsign":"Le AlphaDelta","time":1487455628374.1194}]}') 
>>> print j['users'] 
[{u'ac': u'240', u'co': [26.93526727771073, -79.9251579421954, 10667.999977505215, -163.9999997562341, -2.2288855660652414, 2.4376547336953407e-07], u've': [-2.161617968514662e-06, -6.915441330619387e-07, 4.838653476326726e-06, 0, -2.27708260122718e-10, 0], u'time': 1487455628374.1194, u'st': {u'gr': False}, u'aircraft': u'240', u'callsign': u'Le AlphaDelta', u'ti': 1487455628374.1194, u'cs': u'Le AlphaDelta', u'acid': u'553', u'id': u'553'}] 
>>> print j['users'][0]['ac'] 
240 
>>> print j['users'][0]['cs'] 
Le AlphaDelta