【python基础学习】---解析多层json,解析xml

1、以豆瓣的API接口为例子,解析返回的json数据

https://api.douban.com/v2/book/1220502

{
  "rating":{
    "max":10,
    "numRaters":16,
    "average":"7.3",
    "min":0
  },
  "subtitle":"",
  "author":[
    "Ranjan Bose",
    "武传坤"
  ],
  "pubdate":"2005-1",
  "tags":[
    {
      "count":29,
      "name":"信息论",
      "title":"信息论"
    },
    {
      "count":22,
      "name":"密码学",
      "title":"密码学"
    },
    {
      "count":10,
      "name":"信息论与编码",
      "title":"信息论与编码"
    },
    {
      "count":8,
      "name":"计算机",
      "title":"计算机"
    },
    {
      "count":5,
      "name":"信息学",
      "title":"信息学"
    },
    {
      "count":1,
      "name":"通信",
      "title":"通信"
    },
    {
      "count":1,
      "name":"系统信息论",
      "title":"系统信息论"
    },
    {
      "count":1,
      "name":"欣",
      "title":"欣"
    }
  ],
  "origin_title":"",
  "image":"https://img3.doubanio.com/view/subject/m/public/s1166731.jpg",
  "binding":"平装(无盘)",
  "translator":[
    "武传坤"
  ],
  "catalog":"",
  "pages":"206",
  "images":{
    "small":"https://img3.doubanio.com/view/subject/s/public/s1166731.jpg",
    "large":"https://img3.doubanio.com/lpic/s1166731.jpg",
    "medium":"https://img3.doubanio.com/view/subject/m/public/s1166731.jpg"
  },
  "alt":"https://book.douban.com/subject/1220502/",
  "id":"1220502",
  "publisher":"机械工业出版社",
  "isbn10":"7111155343",
  "isbn13":"9787111155348",
  "title":"信息论、编码与密码学",
  "url":"https://api.douban.com/v2/book/1220502",
  "alt_title":"",
  "author_intro":"",
  "summary":"本书集中介绍了信息论、信源编码、信道编码和密码等方面的知识,不仅内容丰富,而且技术深度适当。适合作为高等学校信息安全、电子工程及相关专业信息论和编码课程的教材,从事相关工作的专业技术人员,也能从中受益。
  本书利用简短的篇幅对信息论、编码与密码学等信息安全方面的知识,及其所涉及的数学理论进行了精辟论述,内容丰富,避免了太数学化而造成的晦涩难懂:通过翔实的例证由浅入深地阐明理论。
   本书特点
   对网格编码调制(TCM)进行了详细介绍,以加性白高斯操声(AWGN)和衰退信道为重点。
   通过实例阐述了信源编码和信道编码方面的知识。
   涵盖了密码学基本理论、密钥和公钥密码学、现代加密标准和最新的研究趋势。",
  "series":{
    "id":"1163",
    "title":"计算机科学丛书"
  },
  "price":"29.00元"
}

python2的解析代码:

import urllib2
import json
html = urllib2.urlopen(r'https://api.douban.com/v2/book/1220562')
hjson = json.loads(html.read())
print hjson['id']
print hjson['rating']['max']
print hjson['tags'][0]['name']

python3的解析代码:

import urllib.request
import json

if __name__ == '__main__':
    html = urllib.request.urlopen(r'https://api.douban.com/v2/book/1220502')
    hjson = json.load(html)
    print(hjson)
    # print(json.dump(hjson).replace('\'','\"'))
    print(hjson['series']['title'])

 二、python的字典(dict)转化为字符串(str):

s = '{"host":"192.168.11.22", "port":3306, "user":"abc",\
                  "passwd":"123", "db":"mydb", "connect_timeout":10}'
print(type(s))
d = json.loads(s)
print(type(d))
______________________________
控制台输入:
<class 'str'>
<class 'dict'>

 三、读取文件json,并解析

zhou.txt内容如下:

{"data":[{"id":"1134","name":"中石油加油站 (建港邮政所西南)","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区鹦鹉大道火车道南侧","brandname":"中石油","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.25006,30.516361","lon":"114.25649912071","lat":"30.522662167959","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":10203},{"id":"8895","name":"中石化琴台加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区琴台路49号,琴台大道与江城大道交叉","brandname":"中石化","type":"直营店","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.250105,30.555689","lon":"114.25651172095","lat":"30.561981100464","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":10258},{"id":"52166","name":"江钻邓甲村天然气加气站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区马鹦路与江腾苑路交叉处,中青假日酒店附近","brandname":"不详","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.2450171803,30.531668884","lon":"114.257943","lat":"30.544322","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":null,"fwlsmc":"","distance":9862},{"id":"53486","name":"道达尔加油站","area":"430033","areaname":"湖北省 武汉市 硚口区","address":"湖北省武汉市硚口区硚口路与仁寿路交叉处,东辉花园对面","brandname":"不详","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.2517841979,30.5741290141","lon":"114.25819031949","lat":"30.580404505826","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"","distance":10760},{"id":"52165","name":"中石油马鹦路加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区马鹦路与江腾苑路交叉处,中青假日酒店附近","brandname":"中石油","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.2453546712,30.5315844542","lon":"114.258255","lat":"30.544189","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":9831},{"id":"8270","name":"中石化金家墩加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市江汉区银墩街,武汉邮政局附近","brandname":"中石化","type":"直营店","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.251981,30.612361","lon":"114.25841829676","lat":"30.618641616727","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":13064},{"id":"51463","name":"中石油金家墩加油站","area":"430040","areaname":"湖北省 武汉市 江汉区","address":"湖北省武汉市江汉区银墩路与银墩街交叉处,湖北邮政全夜航速递局附近 ","brandname":"中石油","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.251983975,30.6154153127","lon":"114.25842360375","lat":"30.621697203353","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":13291},{"id":"49927","name":"道达尔常青路加油站","area":"430040","areaname":"湖北省 武汉市 江汉区","address":"湖北省武汉市江汉区常青路35号,常青路与云彩路交叉口以西方向","brandname":"道达尔","type":"直营店","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.253907,30.606678","lon":"114.26034508395","lat":"30.612935608283","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"银联卡,信用卡支付,加油卡,便利店","distance":12511},{"id":"8892","name":"中石化五里加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区汉阳大道316号","brandname":"中石化","type":"直营店","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.254505,30.550811","lon":"114.26092817171","lat":"30.557056799261","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":9732},{"id":"47702","name":"中石化玉带门加油站","area":"430033","areaname":"湖北省 武汉市 硚口区","address":"湖北省武汉市硚口区京汉大道建国路特1号(祟仁路至桥口方向,地铁集团附近)","brandname":"中石化","type":"直营店","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.254516,30.571964","lon":"114.26093030604","lat":"30.578207291699","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡,便利店","distance":10421},{"id":"3248","name":"中石化青年路加油站","area":"430033","areaname":"湖北省 武汉市 硚口区","address":"湖北省武汉市桥口区解放大道585号,解放大道与航空路交叉","brandname":"中石化","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.256956,30.589896","lon":"114.26338833234","lat":"30.596109715243","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":11161},{"id":"3250","name":"中石化青年路加油站","area":"430033","areaname":"湖北省 武汉市 硚口区","address":"湖北省武汉市硚口区建设大道与航空路路口东200米路北","brandname":"中石化","type":"其他","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.250589627,30.5836655797","lon":"114.26354618034","lat":"30.596405592512","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"E93#":"5.7"},"fwlsmc":"加油卡","distance":11166},{"id":"52161","name":"两湖路加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区两湖路与新五里后堤交叉处,芳草苑附近","brandname":"不详","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.2582592582,30.5212424922","lon":"114.26472225948","lat":"30.527444991614","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"","distance":9330},{"id":"54720","name":"中石化白沙洲北油气合建站","area":"430061","areaname":"湖北省 武汉市 武昌区","address":"湖北省武汉市武昌区三环线白沙洲服务区内北侧","brandname":"中石化","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.2650400597,30.4729104496","lon":"114.27150951472","lat":"30.478997158986","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":11000},{"id":"8893","name":"中石化白沙洲南站","area":"430061","areaname":"湖北省 武汉市 武昌区","address":"湖北省武汉市武昌区三环线下桥处,湖场村附近","brandname":"中石化","type":"其他","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.26513,30.471974","lon":"114.27159905729","lat":"30.478058363651","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡,便利店","distance":11060},{"id":"10495","name":"中石化岔马路加油站","area":"430040","areaname":"湖北省 武汉市 江汉区","address":"湖北省武汉市江汉区解放大道1632号,解放大道与青年路交叉","brandname":"中石化","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.266801,30.580653","lon":"114.27326931334","lat":"30.586705763923","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":9801},{"id":"1137","name":"中石化交通加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区鹦鹉大道与新民一街交叉口,路东","brandname":"中石化","type":"其他","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.260807775,30.5297201838","lon":"114.273748","lat":"30.541908","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"E93#":"5.7"},"fwlsmc":"加油卡","distance":8340},{"id":"47699","name":"中石油鹦鹉加油站","area":"430050","areaname":"湖北省 武汉市 汉阳区","address":"湖北省武汉市汉阳区鹦鹉大道与龟山北路一号,鹦鹉音像制品厂内","brandname":"中石油","type":"直营店","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.267544,30.555849","lon":"114.27401798863","lat":"30.561889149583","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"银联卡,信用卡支付,加?涂?,便利店,发卡充值网点,银联卡充值,加油卡充值业务","distance":8629},{"id":"303","name":"中石化石桥加油站","area":"430040","areaname":"湖北省 武汉市 江汉区","address":"湖北省武汉市江汉区路姑嫂树路和兴业路交叉口向南,路东","brandname":"中石化","type":"其他","discount":"打折加油站","exhaust":"国Ⅳ","position":"114.267576,30.624646","lon":"114.27408293469","lat":"30.630694367322","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":12979},{"id":"16573","name":"北湖西路三丰加油站 ","area":"430040","areaname":"湖北省 武汉市 江汉区","address":"湖北省武汉市江汉区北湖西路薄利通信对面","brandname":"不详","type":"其他","discount":"非打折加油站","exhaust":"国Ⅳ","position":"114.268116,30.603663","lon":"114.27461064049","lat":"30.609697747818","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#车柴":"5.35","E93#":"5.7"},"fwlsmc":"","distance":11245}],"pageinfo":{"pnums":20,"current":"1","allpage":8}}

 判断是否为字典或者列表:

def fordict(contents):
    for key in contents.keys():
        # print(key)
        print(contents[key])
        #判断是否为列表
        if key == 'data' and isinstance(contents[key],list):
            for dd in range(len(contents[key])):
                # print(contents[key][dd])
                #判断是否为字典
                if contents[key][dd]['id'] == '1134' and  isinstance(contents[key][dd],dict):
                    print(fordict(contents[key][dd]))
                else:
                    return

使用递归:

if __name__ == '__main__':
    f = open("zhou.txt","r",encoding='utf-8')
    contents = json.load(f,encoding='utf-8')
    #是否为字段
    print(isinstance(contents,dict))
    fordict(contents)

 

 
posted @ 2018-04-08 15:25  freelan  阅读(8633)  评论(0编辑  收藏  举报