博客
关于我
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/

你可能感兴趣的文章
Netty工作笔记0014---Buffer类型化和只读
查看>>
Netty工作笔记0050---Netty核心模块1
查看>>
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
Netty常见组件二
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>