A new template-approach was demonstrated to fabricate N-doped graphitic nanocages (NGNs) with designable doped structure for efficiently electrocatalyzing oxygen reduction and their graphitic structure has been prepared with in situ N-doping from floating catalytic pyrolysis of N-precursors, catalysts and carbon resources. Because of short time pyrolysis, the process of pyrolyzing N-precursors is incomplete, leading to the structure of N-doping is made up from inheriting incompletely pyrolyzed the precursor. Thus, its structure can be optimized by using a special precursor in this approach. Compared with nanocages prepared from acetonitrile, NGNs prepared from imidazole or pyrrole have pyrrolic structures, leading to their more efficient catalysis for oxygen reduction reaction (ORR). Furthermore, compared with N-doped structure prepared from pyrrole, the structure prepared from imidazole has one more N atom in each pyrrolic ring, which enhances O2 bridge adsorption and leads to the efficient ORR.