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

本文共 3147 字,大约阅读时间需要 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_price
price = get_price(data_iphone, '_widget_1557886562335')
print(price)

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

你可能感兴趣的文章
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>