我有一个用rpi创建的json文件(如下所示),我正在查看获取数组中项目的数量(在这种情况下,是在查看十六进制出现的次数)
JSON文件
{
"now": 1558947116.6,
"messages": 44571282,
"aircraft": [{
"hex": "405658",
"alt_baro": 6775,
"ias": 219,
"mach": 0.376,
"mag_heading": 75.4,
"baro_rate": -1216,
"geom_rate": -1344,
"squawk": "0405",
"mlat": [],
"tisb": [],
"messages": 48,
"seen": 0.1,
"rssi": -18.0
}, {
"hex": "4072a0",
"flight": "BEE7TL ",
"alt_baro": 23975,
"gs": 278.0,
"ias": 206,
"tas": 302,
"mach": 0.488,
"track": 334.0,
"roll": -0.4,
"mag_heading": 320.6,
"baro_rate": 1184,
"geom_rate": 928,
"squawk": "1440",
"nav_altitude": 24000,
"mlat": [],
"tisb": [],
"messages": 195,
"seen": 0.1,
"rssi": -19.1
}, {
"hex": "400a5b",
"flight": "SHT17Q ",
"alt_baro": 37000,
"alt_geom": 36825,
"gs": 460.6,
"ias": 252,
"tas": 450,
"mach": 0.780,
"track": 155.8,
"track_rate": -0.03,
"roll": -0.2,
"mag_heading": 163.7,
"baro_rate": 160,
"geom_rate": 64,
"squawk": "4464",
"category": "A0",
"nav_qnh": 1006.0,
"nav_altitude": 37008,
"lat": 53.376617,
"lon": -2.895996,
"nic": 8,
"rc": 186,
"seen_pos": 0.1,
"version": 0,
"nac_v": 2,
"mlat": [],
"tisb": [],
"messages": 1949,
"seen": 0.1,
"rssi": -12.4
}, {
"hex": "a895eb",
"flight": "UAL940 ",
"alt_baro": 35000,
"alt_geom": 34925,
"gs": 530.7,
"ias": 281,
"tas": 476,
"mach": 0.824,
"track": 104.8,
"track_rate": 0.03,
"roll": 0.5,
"mag_heading": 110.4,
"baro_rate": 0,
"geom_rate": 0,
"squawk": "6323",
"emergency": "none",
"category": "A5",
"nav_qnh": 1013.6,
"nav_altitude": 35008,
"nav_heading": 106.9,
"lat": 53.059915,
"lon": -2.493426,
"nic": 8,
"rc": 186,
"seen_pos": 0.9,
"version": 2,
"nic_baro": 1,
"nac_p": 9,
"nac_v": 1,
"sil": 3,
"sil_type": "perhour",
"gva": 2,
"sda": 2,
"mlat": [],
"tisb": [],
"messages": 5093,
"seen": 0.1,
"rssi": -3.8
}, {
"hex": "4074b4",
"flight": "EZY93QG ",
"alt_baro": 34000,
"alt_geom": 34100,
"gs": 410.4,
"ias": 259,
"tas": 430,
"mach": 0.748,
"track": 322.8,
"track_rate": 0.00,
"roll": 0.0,
"mag_heading": 315.4,
"baro_rate": 0,
"geom_rate": 32,
"squawk": "2720",
"emergency": "none",
"category": "A3",
"nav_qnh": 1013.6,
"nav_altitude": 34016,
"nav_heading": 0.0,
"lat": 52.728137,
"lon": -1.090864,
"nic": 8,
"rc": 186,
"seen_pos": 0.2,
"version": 2,
"nic_baro": 1,
"nac_p": 9,
"nac_v": 1,
"sil": 3,
"sil_type": "perhour",
"gva": 2,
"sda": 2,
"mlat": [],
"tisb": [],
"messages": 4134,
"seen": 0.1,
"rssi": -10.3
}, {
"hex": "4ca4ed",
"alt_baro": 38000,
"gs": 394.2,
"track": 285.4,
"baro_rate": 832,
"lat": 51.823487,
"lon": -2.558136,
"nic": 0,
"rc": 0,
"seen_pos": 45.6,
"nac_v": 0,
"mlat": ["gs", "track", "baro_rate", "lat", "lon", "nic", "rc", "nac_v"],
"tisb": [],
"messages": 95,
"seen": 1.3,
"rssi": -21.6
}, {
"hex": "406a99",
"alt_baro": 9000,
"squawk": "6357",
"lat": 51.813538,
"lon": -2.090075,
"nic": 9,
"rc": 75,
"seen_pos": 55.6,
"version": 2,
"sil_type": "perhour",
"mlat": [],
"tisb": [],
"messages": 559,
"seen": 49.2,
"rssi": -22.1
}, {
"hex": "710111",
"mlat": [],
"tisb": [],
"messages": 49,
"seen": 257.5,
"rssi": -23.3
}, {
"hex": "392aef",
"flight": "AFR1669 ",
"alt_baro": 20750,
"alt_geom": 20675,
"gs": 430.0,
"ias": 302,
"tas": 408,
"mach": 0.664,
"track": 143.8,
"track_rate": 0.00,
"roll": 0.0,
"mag_heading": 151.7,
"baro_rate": 2112,
"geom_rate": 2048,
"squawk": "6301",
"category": "A0",
"nav_qnh": 1009.0,
"nav_altitude": 31008,
"lat": 52.662918,
"lon": -2.054367,
"nic": 7,
"rc": 371,
"seen_pos": 0.3,
"version": 0,
"nac_v": 2,
"mlat": [],
"tisb": [],
"messages": 7400,
"seen": 0.0,
"rssi": -12.2
}, {
"hex": "400cee",
"mlat": [],
"tisb": [],
"messages": 114,
"seen": 91.0,
"rssi": -22.6
}
}
}
答案 0 :(得分:2)
一个简单的解决方案是将JSON
转换为String
,然后使用.count()
方法计算'hex'的频率
import json
data = {
"now": 1558947116.6,
"messages": 44571282,
"aircraft": [
{
"hex": "405658",
"alt_baro": 6775,
"ias": 219,
"mach": 0.376,
"mag_heading": 75.4,
"baro_rate": -1216,
"geom_rate": -1344,
"squawk": "0405",
"mlat": [],
"tisb": [],
"messages": 48,
"seen": 0.1,
"rssi": -18.0
},
{
"hex": "4072a0",
"flight": "BEE7TL ",
"alt_baro": 23975,
"gs": 278.0,
"ias": 206,
"tas": 302,
"mach": 0.488,
"track": 334.0,
"roll": -0.4,
"mag_heading": 320.6,
"baro_rate": 1184,
"geom_rate": 928,
"squawk": "1440",
"nav_altitude": 24000,
"mlat": [],
"tisb": [],
"messages": 195,
"seen": 0.1,
"rssi": -19.1
},
{
"hex": "400a5b",
"flight": "SHT17Q ",
"alt_baro": 37000,
"alt_geom": 36825,
"gs": 460.6,
"ias": 252,
"tas": 450,
"mach": 0.780,
"track": 155.8,
"track_rate": -0.03,
"roll": -0.2,
"mag_heading": 163.7,
"baro_rate": 160,
"geom_rate": 64,
"squawk": "4464",
"category": "A0",
"nav_qnh": 1006.0,
"nav_altitude": 37008,
"lat": 53.376617,
"lon": -2.895996,
"nic": 8,
"rc": 186,
"seen_pos": 0.1,
"version": 0,
"nac_v": 2,
"mlat": [],
"tisb": [],
"messages": 1949,
"seen": 0.1,
"rssi": -12.4
},
{
"hex": "a895eb",
"flight": "UAL940 ",
"alt_baro": 35000,
"alt_geom": 34925,
"gs": 530.7,
"ias": 281,
"tas": 476,
"mach": 0.824,
"track": 104.8,
"track_rate": 0.03,
"roll": 0.5,
"mag_heading": 110.4,
"baro_rate": 0,
"geom_rate": 0,
"squawk": "6323",
"emergency": "none",
"category": "A5",
"nav_qnh": 1013.6,
"nav_altitude": 35008,
"nav_heading": 106.9,
"lat": 53.059915,
"lon": -2.493426,
"nic": 8,
"rc": 186,
"seen_pos": 0.9,
"version": 2,
"nic_baro": 1,
"nac_p": 9,
"nac_v": 1,
"sil": 3,
"sil_type": "perhour",
"gva": 2,
"sda": 2,
"mlat": [],
"tisb": [],
"messages": 5093,
"seen": 0.1,
"rssi": -3.8
},
{
"hex": "4074b4",
"flight": "EZY93QG ",
"alt_baro": 34000,
"alt_geom": 34100,
"gs": 410.4,
"ias": 259,
"tas": 430,
"mach": 0.748,
"track": 322.8,
"track_rate": 0.00,
"roll": 0.0,
"mag_heading": 315.4,
"baro_rate": 0,
"geom_rate": 32,
"squawk": "2720",
"emergency": "none",
"category": "A3",
"nav_qnh": 1013.6,
"nav_altitude": 34016,
"nav_heading": 0.0,
"lat": 52.728137,
"lon": -1.090864,
"nic": 8,
"rc": 186,
"seen_pos": 0.2,
"version": 2,
"nic_baro": 1,
"nac_p": 9,
"nac_v": 1,
"sil": 3,
"sil_type": "perhour",
"gva": 2,
"sda": 2,
"mlat": [],
"tisb": [],
"messages": 4134,
"seen": 0.1,
"rssi": -10.3
},
{
"hex": "4ca4ed",
"alt_baro": 38000,
"gs": 394.2,
"track": 285.4,
"baro_rate": 832,
"lat": 51.823487,
"lon": -2.558136,
"nic": 0,
"rc": 0,
"seen_pos": 45.6,
"nac_v": 0,
"mlat": [
"gs",
"track",
"baro_rate",
"lat",
"lon",
"nic",
"rc",
"nac_v"
],
"tisb": [],
"messages": 95,
"seen": 1.3,
"rssi": -21.6
},
{
"hex": "406a99",
"alt_baro": 9000,
"squawk": "6357",
"lat": 51.813538,
"lon": -2.090075,
"nic": 9,
"rc": 75,
"seen_pos": 55.6,
"version": 2,
"sil_type": "perhour",
"mlat": [],
"tisb": [],
"messages": 559,
"seen": 49.2,
"rssi": -22.1
},
{
"hex": "710111",
"mlat": [],
"tisb": [],
"messages": 49,
"seen": 257.5,
"rssi": -23.3
},
{
"hex": "392aef",
"flight": "AFR1669 ",
"alt_baro": 20750,
"alt_geom": 20675,
"gs": 430.0,
"ias": 302,
"tas": 408,
"mach": 0.664,
"track": 143.8,
"track_rate": 0.00,
"roll": 0.0,
"mag_heading": 151.7,
"baro_rate": 2112,
"geom_rate": 2048,
"squawk": "6301",
"category": "A0",
"nav_qnh": 1009.0,
"nav_altitude": 31008,
"lat": 52.662918,
"lon": -2.054367,
"nic": 7,
"rc": 371,
"seen_pos": 0.3,
"version": 0,
"nac_v": 2,
"mlat": [],
"tisb": [],
"messages": 7400,
"seen": 0.0,
"rssi": -12.2
},
{
"hex": "400cee",
"mlat": [],
"tisb": [],
"messages": 114,
"seen": 91.0,
"rssi": -22.6
}
]
}
data = json.dumps(data) # Convert JSON to String
hex_count = data.count('hex') # Count Frequency of 'hex'
答案 1 :(得分:1)
如果使用json
模块加载文件,则可以遍历aircraft
列表中的条目并计算hex
在字典中的次数:>
import json
with open('yourfile.json') as fh: # open the file
data = json.load(fh) # load it into memory as dictionary
hexes = 0 # initialize this at 0 to avoid NameErrors
for craft in data['aircraft']: # iterate over the entries
if 'hex' in craft: # check for key in dictionary
hexes += 1 # if that key exists, it will increment hexes
hexes
# 10
或者,更简洁地说,您可以对sum
使用列表推导:
hexes = sum([1 for craft in data['aircraft'] if 'hex' in craft])
对于每一个包含密钥list
的工艺品,您将在1
中获得'hex'
的{{1}},而您只需sum
就可以得到
答案 2 :(得分:1)
您可以使用加载的json将其添加到计数器中:
count = 0
datastore = json.loads(json_string)
for air in datastore['aircraft']:
if 'hex' in air:
count += 1
print(count)
或使用带有lamda的过滤器进行检查,然后获取长度
d = json.loads(json_string)
aircrafts = d['aircraft']
count1 = len(list(filter(lambda x: 'hex' in x, aircrafts)))
print(count1)