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

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

Python与JSON数据处理指南

在处理JSON数据时,Python提供了强大的json模块,能够方便地进行数据的序列化与反序列化。以下是关于JSON与Python数据类型对应关系的详细说明,以及如何将给定的JSON数据转换为Python数据结构。

JSON与Python数据类型对应关系

  • JSON数据类型 | Python数据类型
  • object | dict
  • array | list
  • number | int/float
  • true/false/null | Python布尔值/None

数据处理步骤

import json# 将JSON数据转换为Python字典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"        }    ]}'''# 转换JSON为Python数据data_iphone = json.loads(data_iphone_json)# 提取iPhone 8的商品数量num_iphone8 = data_iphone['data'][2]['_widget_1557886562350']print(num_iphone8)# 提取所有商品价格def get_price(data_iphone_products, product_key):    product_price = []    for products in data_iphone_products['data']:        product_price.append(products[product_key])    return product_priceprice = get_price(data_iphone, '_widget_1557886562335')print(price)

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

你可能感兴趣的文章
NodeJs连接Oracle数据库
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
Nodemon 深入解析与使用
查看>>
node不是内部命令时配置node环境变量
查看>>
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>
Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
查看>>
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>