如何基于python操作json文件获取内容
作者:ansonwan 时间:2023-03-30 15:48:04
这篇文章主要介绍了如何基于python操作json文件获取内容,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
写case时,将case 写到json文件比写到,写python一定要学会处理json
以下,是要处理的json
处理操作包括:打开json文件,获取json文件内容,关闭json文件,读取内容中的对应key的value
{
"name": "BeJson",
"url": "http://www.bejson.com",
"page": 88,
"isNonProfit": true,
"address": {
"street": "科技园路.",
"city": "江苏苏州",
"country": "中国"
},
"links": [
{
"name": "Google",
"url": "http://www.google.com"
},
{
"name": "Baidu",
"url": "http://www.baidu.com"
},
{
"name": "SoSo",
"url": "http://www.SoSo.com"
}
]
}
python实现:
#coding=utf-8
import json
class OperationJson:
def __init__(self,file_name=None):
if file_name:
self.file_name = file_name
else:
self.file_name = './dataConfig/data.json'
self.data = self.get_data()
def get_data(self):
fp = open(self.file_name)
data = json.load(fp)
fp.close()
return data
def get_value(self,id):
return self.data[id]
if __name__ == '__main__':
opers = OperationJson()
print opers.get_value('name')
来源:https://www.cnblogs.com/ansonwan/p/12077179.html
标签:python,操作,json,获取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytorch torchvision.ImageFolder的用法介绍
2023-05-29 17:20:49
![](https://img.aspxhome.com/file/2023/4/92454_0s.jpg)
Django之PopUp的具体实现方法
2023-03-26 13:04:54
剖析网页设计中的几何圆
2010-10-19 12:27:00
![](https://img.aspxhome.com/file/UploadPic/201010/19/01-46s.jpg)
Python+pandas编写命令行脚本操作excel的tips详情
2023-09-23 22:35:53
![](https://img.aspxhome.com/file/2023/9/76989_0s.png)
PHP5 mysqli的prepare准备语句使用说明
2023-11-22 12:50:29
http状态码一览表以及HTTP响应的返回头信息
2010-03-31 14:45:00
有关JS中Event对象的几点总结
2009-03-06 12:36:00
vue中provide和inject的用法及说明(vue组件爷孙传值)
2024-05-21 10:15:26
![](https://img.aspxhome.com/file/2023/6/125296_0s.png)
python书籍信息爬虫实例
2023-11-17 11:15:49
asp如何编写一个最简单的聊天程序?
2009-11-08 19:04:00
Python学习笔记之文件的读写操作实例分析
2023-08-28 04:29:06
python脚本替换指定行实现步骤
2022-03-18 16:53:28
Python爬虫新手入门之初学lxml库
2021-11-19 07:16:29
![](https://img.aspxhome.com/file/2023/2/124952_0s.jpg)
在ASP中使用SQL语句之3:LIKE、NOT LIKE和 BETWEEN
2007-08-11 12:30:00
在ASP.NET 2.0中操作数据之十:使用 GridView和DetailView实现的主/从报表
2023-07-02 20:22:40
![](https://img.aspxhome.com/file/2023/6/76336_0s.png)
ERROR CODE: 1175 YOU ARE USING SAFE UPDATE MODE AN
2024-01-17 21:57:50
![](https://img.aspxhome.com/file/2023/1/108911_0s.png)
解决python3捕获cx_oracle抛出的异常错误问题
2023-01-21 17:51:26
SQL Server存储过程中编写事务处理的方法小结
2024-01-15 15:24:24
MySQL replace函数替换字符串语句的用法
2024-01-12 16:56:29
ASP下检测图片木马的函数代码
2011-02-05 10:43:00