Python3 获取以及解析json格式

获取到某个页面的json数据

import requests
import json url
= 'http://192.168.200.99:8000/student/' response = requests.get(url) print(response.text)
print(type(response.text))

数据结果:

{"code": 1, "data": [{"sno": 0, "name": "\u4ee3\u4fca\u8d85", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "2434147365@qq.com", "address": "\u91cd\u5e86\u5e02\u4e5d\u9f99\u5761\u533a\u671d\u9633\u8def", "image": null}, {"sno": 1, "name": "\u5c0f\u7cd6\u59d0\u59d0", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "XiangTang@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 2, "name": "\u8d85\u54e5", "gender": "\u5973", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Jiangchaolan@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 3, "name": "\u9ad8\u5cf0", "gender": "\u7537", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Gaofeng@qq.com", "address": "\u91cd\u5e86\u5e02\u6e1d\u4e2d\u533a\u6e1d\u5dde\u8def\u77f3\u6865\u5e7f\u573a", "image": null}, {"sno": 4, "name": "\u738b\u5ddd", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Wangchuan@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u5317\u533a", "image": null}, {"sno": 5, "name": "\u7f57\u8001\u5e08", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Louyinxia@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 6, "name": "\u6731\u4fca", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Louyouxiang@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 7, "name": "\u5f6d\u5b9d\u9510", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "CC@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 8, "name": "\u5f20\u771f\u94ed", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Liujun@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u77f3\u87c6\u9547", "image": null}, {"sno": 9, "name": "\u9f9a\u91d1\u94ed", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Huhaiyuan@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 10, "name": "\u9648\u79d1", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Wangchao@qq.com", "address": "\u91cd\u5e86\u5e02\u5317\u789a\u533a", "image": null}, {"sno": 11, "name": "\u4f55\u7a0b\u7a0b", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Tangchaolizi@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 12, "name": "\u949f\u6587\u607a", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Yuxiangrousi@qq.com", "address": "\u91cd\u5e86\u5e02\u4e5d\u9f99\u5761\u533a\u671d\u9633\u8def", "image": null}, {"sno": 1000, "name": "\u4ee3\u4fca\u8d85", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "2434147365@qq.com", "address": "\u91cd\u5e86\u5e02\u4e5d\u9f99\u5761\u533a\u671d\u9633\u8def", "image": null}, {"sno": 1001, "name": "\u5c0f\u7cd6\u59d0\u59d0", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "XiangTang@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 1002, "name": "\u6c5f\u8d85\u5c9a", "gender": "\u5973", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Jiangchaolan@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 1003, "name": "\u9ad8\u5cf0", "gender": "\u7537", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Gaofeng@qq.com", "address": "\u91cd\u5e86\u5e02\u6e1d\u4e2d\u533a\u6e1d\u5dde\u8def\u77f3\u6865\u5e7f\u573a", "image": null}, {"sno": 1004, "name": "\u738b\u5ddd", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Wangchuan@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u5317\u533a", "image": null}, {"sno": 1005, "name": "\u7f57\u8001\u5e08", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Louyinxia@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 1006, "name": "\u7f57\u4f51\u7965", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Louyouxiang@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 1007, "name": "CC", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "CC@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 1008, "name": "\u5218\u4fca", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Liujun@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u77f3\u87c6\u9547", "image": null}, {"sno": 1009, "name": "\u80e1\u6d77\u5143", "gender": "\u7537", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Huhaiyuan@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 10010, "name": "\u738b\u8d85", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Wangchao@qq.com", "address": "\u91cd\u5e86\u5e02\u5317\u789a\u533a", "image": null}, {"sno": 10011, "name": "\u7cd6\u7092\u6817\u5b50", "gender": "\u5973", "birthday": "1998-09-15", "mobile": "18580748731", "email": "Tangchaolizi@qq.com", "address": "\u91cd\u5e86\u5e02\u6c5f\u6d25\u533a\u767d\u6c99\u9547", "image": null}, {"sno": 10012, "name": "\u9c7c\u9999\u8089\u4e1d", "gender": "\u7537", "birthday": "1998-09-14", "mobile": "18580748731", "email": "Yuxiangrousi@qq.com", "address": "\u91cd\u5e86\u5e02\u4e5d\u9f99\u5761\u533a\u671d\u9633\u8def", "image": null}]}
<class 'str'>

此时的数据为字符串格式,需要改为字典格式 利用json.loads方法

import requests,json

url = 'http://192.168.200.99:8000/student/'
response = requests.get(url)
res = json.loads(response.text)
print(res)
print(type(res))

数据结果:

{'code': 1, 'data': [{'sno': 0, 'name': '代俊超', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': '2434147365@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}, {'sno': 1, 'name': '小糖姐姐', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'XiangTang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 2, 'name': '超哥', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Jiangchaolan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 3, 'name': '高峰', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Gaofeng@qq.com', 'address': '重庆市渝中区渝州路石桥广场', 'image': None}, {'sno': 4, 'name': '王川', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchuan@qq.com', 'address': '重庆市江北区', 'image': None}, {'sno': 5, 'name': '罗老师', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Louyinxia@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 6, 'name': '朱俊', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Louyouxiang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 7, 'name': '彭宝锐', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'CC@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 8, 'name': '张真铭', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Liujun@qq.com', 'address': '重庆市江津区石蟆镇', 'image': None}, {'sno': 9, 'name': '龚金铭', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Huhaiyuan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 10, 'name': '陈科', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchao@qq.com', 'address': '重庆市北碚区', 'image': None}, {'sno': 11, 'name': '何程程', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Tangchaolizi@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 12, 'name': '钟文恺', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Yuxiangrousi@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}, {'sno': 1000, 'name': '代俊超', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': '2434147365@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}, {'sno': 1001, 'name': '小糖姐姐', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'XiangTang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1002, 'name': '江超岚', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Jiangchaolan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1003, 'name': '高峰', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Gaofeng@qq.com', 'address': '重庆市渝中区渝州路石桥广场', 'image': None}, {'sno': 1004, 'name': '王川', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchuan@qq.com', 'address': '重庆市江北区', 'image': None}, {'sno': 1005, 'name': '罗老师', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Louyinxia@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1006, 'name': '罗佑祥', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Louyouxiang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1007, 'name': 'CC', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'CC@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1008, 'name': '刘俊', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Liujun@qq.com', 'address': '重庆市江津区石蟆镇', 'image': None}, {'sno': 1009, 'name': '胡海元', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Huhaiyuan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 10010, 'name': '王超', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchao@qq.com', 'address': '重庆市北碚区', 'image': None}, {'sno': 10011, 'name': '糖炒栗子', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Tangchaolizi@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 10012, 'name': '鱼香肉丝', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Yuxiangrousi@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}]}
<class 'dict'>

现在得到想要的格式了

如何获取code值?

print(res['code'])

如何获取data值? 因为此时data的值为一个列表,所以需要把data出来对它进行循环

for i in res['data']:
    print(i)

数据结果:

{'sno': 0, 'name': '代俊超', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': '2434147365@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}
{'sno': 1, 'name': '小糖姐姐', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'XiangTang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}
{'sno': 2, 'name': '超哥', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Jiangchaolan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}
{'sno': 3, 'name': '高峰', 'gender': '', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Gaofeng@qq.com', 'address': '重庆市渝中区渝州路石桥广场', 'image': None}
{'sno': 4, 'name': '王川', 'gender': '', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchuan@qq.com', 'address': '重庆市江北区', 'image': None}

如何获取到里面的name值与mobile值?

for i in res['data']:
    print(i['name'], i['mobile'])

数据结果:

代俊超 18580748731
小糖姐姐 18580748731
超哥 18580748731
高峰 18580748731

 

 

 

 

 

 

{'code': 1, 'data': [{'sno': 0, 'name': '代俊超', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': '2434147365@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}, {'sno': 1, 'name': '小糖姐姐', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'XiangTang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 2, 'name': '超哥', 'gender': '女', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Jiangchaolan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 3, 'name': '高峰', 'gender': '男', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Gaofeng@qq.com', 'address': '重庆市渝中区渝州路石桥广场', 'image': None}, {'sno': 4, 'name': '王川', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchuan@qq.com', 'address': '重庆市江北区', 'image': None}, {'sno': 5, 'name': '罗老师', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Louyinxia@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 6, 'name': '朱俊', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Louyouxiang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 7, 'name': '彭宝锐', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'CC@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 8, 'name': '张真铭', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Liujun@qq.com', 'address': '重庆市江津区石蟆镇', 'image': None}, {'sno': 9, 'name': '龚金铭', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Huhaiyuan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 10, 'name': '陈科', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchao@qq.com', 'address': '重庆市北碚区', 'image': None}, {'sno': 11, 'name': '何程程', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Tangchaolizi@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 12, 'name': '钟文恺', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Yuxiangrousi@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}, {'sno': 1000, 'name': '代俊超', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': '2434147365@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}, {'sno': 1001, 'name': '小糖姐姐', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'XiangTang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1002, 'name': '江超岚', 'gender': '女', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Jiangchaolan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1003, 'name': '高峰', 'gender': '男', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Gaofeng@qq.com', 'address': '重庆市渝中区渝州路石桥广场', 'image': None}, {'sno': 1004, 'name': '王川', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchuan@qq.com', 'address': '重庆市江北区', 'image': None}, {'sno': 1005, 'name': '罗老师', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Louyinxia@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1006, 'name': '罗佑祥', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Louyouxiang@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1007, 'name': 'CC', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'CC@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 1008, 'name': '刘俊', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Liujun@qq.com', 'address': '重庆市江津区石蟆镇', 'image': None}, {'sno': 1009, 'name': '胡海元', 'gender': '男', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Huhaiyuan@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 10010, 'name': '王超', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Wangchao@qq.com', 'address': '重庆市北碚区', 'image': None}, {'sno': 10011, 'name': '糖炒栗子', 'gender': '女', 'birthday': '1998-09-15', 'mobile': '18580748731', 'email': 'Tangchaolizi@qq.com', 'address': '重庆市江津区白沙镇', 'image': None}, {'sno': 10012, 'name': '鱼香肉丝', 'gender': '男', 'birthday': '1998-09-14', 'mobile': '18580748731', 'email': 'Yuxiangrousi@qq.com', 'address': '重庆市九龙坡区朝阳路', 'image': None}]}<class 'dict'>

posted @ 2020-07-14 10:00  阿久丶xin  阅读(2169)  评论(0编辑  收藏  举报
Live2D