Python json转字典字符方法实例解析


Posted in Python onApril 13, 2020

josn基本操作

1.导入import json

2.字典转json:json.dumps(dict,ensure_ascii=False),加,ensure_ascii=False转换之后无中文乱码

3.json转字典:json.loads(str)

4.json转字典:requests.get().josn()

5.返回字符串: requests.get().text

举例源码

#!/usr/bin/python3
# encoding:utf-8
import json
import requests

class jsonC():
  def __init__(self):
    self.url = 'http://wthrcdn.etouch.cn/weather_mini?city=北京'
    self.geturl = requests.get(self.url)
  
  #字典转json,因为python没json类型所以str表示
  def dict_json(self):
    d = {"name":"张三","age":18}
    j = json.dumps(d,ensure_ascii=False)
    print('dict_json函数:类型:',type(d),'转类型',type(j),'\n',j)
  
  #json转字典  
  def json_dict(self):
    s = '{"name":"张三","age":18}'
    d = json.loads(s)
    print('json_dict函数:类型:',type(s),'转类型',type(d))
    
  #接口调用直接返回 字典(dict) 
  def get_json(self):
    d = self.geturl.json()
    print('get_json函数类型:',type(d))
  
  #接口调用直接返回字符串  
  def get_str(self):
    s = self.geturl.text
    print('get_str函数返回类型:',type(s))
    
if __name__=="__main__":
  js = jsonC()
  js.dict_json()
  js.json_dict()
  js.get_json()
  js.get_str()

运行结果

dict_json函数:类型: <class 'dict'> 转类型 <class 'str'>
{"name": "张三", "age": 18}
json_dict函数:类型: <class 'str'> 转类型 <class 'dict'>
get_json函数类型: <class 'dict'>
get_str函数返回类型: <class 'str'>

调用get例子

http://wthrcdn.etouch.cn/weather_mini?city=北京

返回json值:

{"data":
	{"yesterday":
		{"date":"28日星期六","high":"高温 30℃","fx":"西南风","low":"低温 17℃","fl":"<![CDATA[<3级]]>","type":"晴"},
		"city":"北京","forecast":
		[
			{"date":"29日星期天","high":"高温 29℃","fengli":"<![CDATA[<3级]]>","low":"低温 18℃","fengxiang":"南风","type":"晴"},
			{"date":"30日星期一","high":"高温 28℃","fengli":"<![CDATA[<3级]]>","low":"低温 19℃","fengxiang":"南风","type":"晴"},
			{"date":"1日星期二","high":"高温 29℃","fengli":"<![CDATA[<3级]]>","low":"低温 20℃","fengxiang":"南风","type":"多云"},
			{"date":"2日星期三","high":"高温 29℃","fengli":"<![CDATA[<3级]]>","low":"低温 17℃","fengxiang":"南风","type":"晴"},
			{"date":"3日星期四","high":"高温 30℃","fengli":"<![CDATA[<3级]]>","low":"低温 12℃","fengxiang":"东南风","type":"多云"}
		],"ganmao":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。","wendu":"29"
	},"status":1000,"desc":"OK"
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用Flask框架获取用户IP地址的方法
Mar 21 Python
Python运算符重载详解及实例代码
Mar 07 Python
Python学习小技巧之列表项的排序
May 20 Python
Anaconda多环境多版本python配置操作方法
Sep 12 Python
Python操作MongoDB数据库的方法示例
Jan 04 Python
Python实现多条件筛选目标数据功能【测试可用】
Jun 13 Python
Django的Modelforms用法简介
Jul 27 Python
python设置随机种子实例讲解
Sep 12 Python
python提取xml里面的链接源码详解
Oct 15 Python
Python爬虫之Selenium下拉框处理的实现
Dec 04 Python
使用scrapy实现增量式爬取方式
Jun 21 Python
python可视化分析绘制带趋势线的散点图和边缘直方图
Jun 25 Python
Jupyter notebook设置背景主题,字体大小及自动补全代码的操作
Apr 13 #Python
Python unittest框架操作实例解析
Apr 13 #Python
windows python3安装Jupyter Notebooks教程
Apr 13 #Python
Python MOCK SERVER moco模拟接口测试过程解析
Apr 13 #Python
JupyterNotebook 输出窗口的显示效果调整方法
Apr 13 #Python
解决Jupyter Notebook开始菜单栏Anaconda下消失的问题
Apr 13 #Python
Python selenium文件上传下载功能代码实例
Apr 13 #Python
You might like
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
2012/06/13 PHP
简单的php数据库操作类代码(增,删,改,查)
2013/04/08 PHP
PHP用反撇号执行外部命令
2015/04/14 PHP
解决yii2左侧菜单子级无法高亮问题的方法
2016/05/08 PHP
Yii2框架操作数据库的方法分析【以mysql为例】
2019/05/27 PHP
NodeJs中的非阻塞方法介绍
2012/06/05 NodeJs
js 上下左右键控制焦点(示例代码)
2013/12/14 Javascript
coffeescript使用的方式汇总
2015/08/05 Javascript
JavaScript位移运算符(无符号) &gt;&gt;&gt; 三个大于号 的使用方法详解
2016/03/31 Javascript
Bootstrap导航条可点击和鼠标悬停显示下拉菜单的实现代码
2016/06/23 Javascript
EditPlus中的正则表达式 实战(2)
2016/12/15 Javascript
jQuery手指滑动轮播效果
2016/12/22 Javascript
bootstarp modal框居中显示的实现代码
2017/02/18 Javascript
js实现可以点击收缩或张开的悬浮窗
2017/09/18 Javascript
微信小程序如何实现全局重新加载
2019/06/05 Javascript
Vue搭建后台系统需要注意的问题
2019/11/08 Javascript
Element-UI+Vue模式使用总结
2020/01/02 Javascript
JavaScript中的this原理及6种常见使用场景详解
2020/02/14 Javascript
纯JS实现五子棋游戏
2020/05/28 Javascript
Vue优化:常见会导致内存泄漏问题及优化详解
2020/08/04 Javascript
微信小程序onShareTimeline()实现分享朋友圈
2021/01/07 Javascript
Python中3种内建数据结构:列表、元组和字典
2014/11/30 Python
Python学习小技巧之利用字典的默认行为
2017/05/20 Python
python逐行读写txt文件的实例讲解
2018/04/03 Python
python使用zip将list转为json的方法
2018/12/31 Python
Python IDLE或shell中切换路径的操作
2020/03/09 Python
使用python从三个角度解决josephus问题的方法
2020/03/27 Python
为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景
2021/01/05 Python
英语系本科生个人求职信
2013/09/21 职场文书
电大毕业生自我鉴定
2013/11/10 职场文书
公司年会晚宴演讲稿
2014/01/06 职场文书
初中教师业务学习材料
2014/05/12 职场文书
python实现黄金分割法的示例代码
2021/04/28 Python
阿里云服务器部署mongodb的详细过程
2021/09/04 MongoDB
微信小程序调用python模型
2022/04/21 Python
Oracle删除归档日志及添加定时任务
2022/06/28 Oracle