Python读取json文件中的数据和统计行数【多测师】

json源数据如下:
{
 "name":"中国",
 "province":[
 {
       "name":"多测师",
       "citys":{
           "city":["上海多测师","上海多测师"]
       }
   },
   {
   "name":"多测师",
       "citys":{
           "city":["上海多测师","深圳多测师","广州多测师"]
       }
   },
   {
   "name":"多测师",
       "citys":{
           "city":["上海多测师","深圳多测师"]
       }
   },
   {
   "name":"多测师",
       "citys":{
           "city":["上海多测师"]
       }
   }
 ]
}

 

#coding=utf-8
"""
===========================
Author:多测师_王sir
Time:2020/6/22 15:04
Wechat:15367499889
Company:上海多测师信息有限公司
===========================
"""

import os

class Json_Utils:

    def __init__(self,path):
        self.path = path

    def read_json(self):
        file = open(self.path,'r')
        all = file.readlines()
        for j in all:
            print j.strip('\n')   #读取json中的文件内容
        num = 0
        for i in all:
            num+=1
        print 'json文件的行数为{0}行'.format(num)

if __name__ == '__main__':
    read = Json_Utils(r'D:\project\dcs2\api_auto\x0x0.json')
    read.read_json()
运行结果如下:


{
 "name":"中国",
 "province":[
 {
       "name":"多测师",
       "citys":{
           "city":["上海多测师","上海多测师"]
       }
   },
   {
   "name":"多测师",
       "citys":{
           "city":["上海多测师","深圳多测师","广州多测师"]
       }
   },
   {
   "name":"多测师",
       "citys":{
           "city":["上海多测师","深圳多测师"]
       }
   },
   {
   "name":"多测师",
       "citys":{
           "city":["上海多测师"]
       }
   }
 ]
}
json文件的行数为29行

 

posted @ 2020-06-22 18:14  多测师_树哥  阅读(4526)  评论(0编辑  收藏  举报