2017-09-28 9 views
0

1.json fichier contient de nombreux paquets WIFI reniflant, je veux obtenir l'adresse mac du récepteur et de l'émetteur qui peut être trouvé dans le premier objet "wlan" appelé "wlan.ra" et "wlan.sa". data [0] est le premier paquet WIFI. Mais quand j'essaye d'imprimer les éléments de wlan après le chargement de json, il montre seulement les éléments du deuxième objet "wlan" donc il n'y a pas "wlan.ra" et "wlan.sa" dans le Les données.comment fusionner même objet dans un fichier json en utilisant python

with open('1.json','r') as json_data: 
    data = json.load(json_data) 
a=data[0] 
print a 

Q2: Il y a deux objets 'wlan' dans mon fichier JSON. Comment puis-je fusionner les éléments de ces deux objets 'wlan' en un seul objet 'wlan'?

Voici mon code, mais il ne fonctionne pas:

with open('1.json','r') as f: 
    data=json.load(f) 
    for i in data: 
     i['_source']['layers']['wlan'].update() 

Capture d'écran de fichier JSON:

Wlan obj - 1.json

Répondre

0
''' 
Created on 2017/10/3 

@author: DD 
''' 

import os 

def modify_jsonfile(jsonfile): 
''' 
replace wlan to wlan1/wlan2 
''' 
FILESUFFIX = '_new' # filename suffix 
LBRACKET = '{' # json object delimiter 
RBRACKET = '}' 
INTERSETED = '"wlan"' # string to be replaced 
nBrackets = 0 # stack to record object status 
nextIndex = 1 # next index of wlan 
with open(jsonfile, 'r') as fromJsonFile: 
    fields = os.path.splitext(jsonfile) # generate new filename 
    with open(fields[0] + FILESUFFIX + fields[1], 'w') as toJsonFile: 
     for line in fromJsonFile.readlines(): 
      for ch in line: # record bracket 
       if ch == LBRACKET: 
        nBrackets += 1 
       elif ch == RBRACKET: 
        nBrackets -= 1 
       if nBrackets == 0: 
        nextIndex = 1 
      if (nextIndex == 1 or nextIndex == 2) and line.strip().find(INTERSETED) == 0: # replace string 
       line = line.replace(INTERSETED, INTERSETED[:-1] + str(nextIndex) + INTERSETED[-1]) 
       nextIndex += 1 
      toJsonFile.write(line); 
print 'done.' 

if __name__ == '__main__': 
jsonfile = r'C:\Users\DD\Desktop\1.json'; 
modify_jsonfile(jsonfile)