博客
关于我
python作业之JSON数据的处理
阅读量:276 次
发布时间:2019-03-01

本文共 3437 字,大约阅读时间需要 11 分钟。

提示:python转化JSON的函数是json.loads()

在这里插入图片描述

提示:把JSON转化为Python数据叫反序列化,同理将Python数据转化为JSON数据叫序列化,下图是JSON和Python数据类型的对应表:

JSON数据类型 Python 数据类型
object dict
array list
number int
number float
true True
false False
null None
true True
# 导入json处理包import json# 下面是一个json格式数据,多行字符串用'''data_iphone_json = '''{    "data": [    {            "creator": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "updater": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "deleter": null,            "createTime": "2020-03-26T02:41:06.491Z",            "updateTime": "2020-03-26T02:46:27.825Z",            "deleteTime": null,            "_widget_1557886562320": "iPhone 11",            "_widget_1557886562335": "5998",            "_widget_1557886562350": "17",            "_id": "5e7c164229e01a00063be284",            "appId": "5e798363b587cc0006b40445",            "entryId": "5cdb765b5a6ae613aeed0f84"        },        {            "creator": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "updater": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "deleter": null,            "createTime": "2020-03-26T02:47:02.037Z",            "updateTime": "2020-03-26T02:47:02.037Z",            "deleteTime": null,            "_widget_1557886562320": "iPhone X",            "_widget_1557886562335": "4998",            "_widget_1557886562350": "5",            "_id": "5e7c17a650bccb0006441778",            "appId": "5e798363b587cc0006b40445",            "entryId": "5cdb765b5a6ae613aeed0f84"        },        {            "creator": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "updater": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "deleter": null,            "createTime": "2020-03-26T02:47:43.059Z",            "updateTime": "2020-03-26T02:47:43.059Z",            "deleteTime": null,            "_widget_1557886562320": "iPhone 8",            "_widget_1557886562335": "3998",            "_widget_1557886562350": "32",            "_id": "5e7c17cfcd87510006cf8189",            "appId": "5e798363b587cc0006b40445",            "entryId": "5cdb765b5a6ae613aeed0f84"        }    ]}'''# 要求1:将数据复制到Python,并赋值给变量data_ iphone) 提示:需要先手动将json数据处理成Python的字典数据# 将json格式转化为python格式data_iphone = json.loads(data_iphone_json)# print(data_iphone)'''要求2:使用Python代码从数据中提取出iPhone 8的商品数量("_widget_ 1557886562350"),并赋值给变量num_ iphone80提示: iphone 8是'data'里面的第3个数据,需要使用data_ iphone['data'][2]进行索引'''num_iphone8 = data_iphone['data'][2]['_widget_1557886562350']print(num_iphone8)'''使用for循环,从数据中提取出所有的商品价格("_widget_1557886562335"),并赋值给变量price (建议price使用列表格式)并用Python去处理数据'''# 获得产品价格,函数导入产品数据,产品的keydef get_price(data_iphone_products, product_key):    # 定义一个价格的列表    product_price = []    # 遍历data列表中的产品    for products in data_iphone_products['data']:        # 将产品的价格添加到价格列表中        product_price.append(products[product_key])    return product_price# 通过函数获得所有商品中的价格price = get_price(data_iphone, '_widget_1557886562335')print(price)

转载地址:http://wldo.baihongyu.com/

你可能感兴趣的文章
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>