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'] == '' 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)
												

【python基础学习】---解析多层json,解析xml的更多相关文章

  1. Python学习--21天Python基础学习之旅(Day01、Day02)

    21天的python基础学习,使用<Python从入门到实践>,并且需要手敲书中的code,以下为整个学习过程的记录. Day01: 安装python时要选择复选框 Add Python ...

  2. Day1 Python基础学习

    一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编写程序,本质仍然是直接操作 ...

  3. Day1 Python基础学习——概述、基本数据类型、流程控制

    一.Python基础学习 一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编 ...

  4. python 基础之pickle 与json 报错问题解决方案

    Python 基础之pickle与json 有没有在搞pickle与json在进行数据储存的时候老是报错,这个有些让人烦恼,在之前有一篇介绍过它们的基本用法以及在使用过长中避免一些坑,但是今天在把对象 ...

  5. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  6. Python 基础学习 总结篇

    Python 基础学习总结 先附上所有的章节: Python学习(一)安装.环境配置及IDE推荐 Python学习(二)Python 简介 Python学习(三)流程控制 Python学习(四)数据结 ...

  7. (一)python基础学习

    根据廖雪峰老师的python教程写一些学习总结! Python基础学习 1.使用list和tuple (1)list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时 ...

  8. python基础学习(起步)

    目录 python基础学习(起步) 变量 常量 变量的内存管理 python垃圾回收机制 变量的其他赋值方式 今日编程小题 本人能力有限,若有偏颇之处请读者大大不吝赐教! 祝大家每天都在成长! pyt ...

  9. Python基础学习二

    Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...

  10. Python基础学习一

    Python基础学习一 1.变量与常量 变量名:大小写英文.数字.下划线的组合,数字不能开头 常量名:习惯上常量用大写字母命名,例如"PI" 2.多行输出 转义符:反斜杠(),如果 ...

随机推荐

  1. 题解 最长上升序列2 — LIS2

    最长上升序列2 - LIS2 Description 已知一个 1 ∼ N 的排列的最长上升子序列长度为 K ,求合法的排列个数. Input 输入一行二个整数 N , K ( K ≤ N ≤ 15) ...

  2. sql 查询列

    select 'A' AS A , B ='B'

  3. Codeforces 1167 E Range Deleting 双指针+思维

    题意 给一个数列\(a​\),定义\(f(l,r)​\)为删除\(a​\)中所有满足\(l<=a_i<=r​\)的数后的数列,问有多少对\((l,r)​\),使\(f(l,r)​\)是一个 ...

  4. 2016 Multi-University Training Contest 4 部分题解

    1001,官方题解是直接dp,首先dp[i]表示到i位置的种类数,它首先应该等于dp[i-1],(假设m是B串的长度)同时,如果(i-m+1)这个位置开始到i这个位置的这一串是和B串相同的,那么dp[ ...

  5. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...

  6. router 配置按需加载对应的组件,配置active

    const routes = [ { path: '/', component: App, children: [ {path: '/index/:type', name: 'index', comp ...

  7. openerp学习笔记 对象调用(创建、修改),用于后台代码创建和更新对象

    #服务卡创建,自动更新服务卡为开卡状态    def create(self, cr, uid, values, context=None):        values['state'] = '1' ...

  8. gateway 整合 websocket demo

    背景: 这个websocket  因为使用的地方不多,并没有独立出一个项目,是集成在已有的服务中. 1: gateway 配置 - id: service-test   uri: lb:ws://se ...

  9. javascript之Screen(屏幕)对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. javascript之Location对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...