python json接口数据提取_使用 python 解析接口返回的 json 数据

目标

通过调用移动之家提供的版本号接口获取当前线上环境 iOS 平台最新版本的版本号。

第一步:获取 http 请求结果 #!/usr/bin/python

# -*- coding: utf-8 -*-

# 使用 requests 库

import requests

response = requests.get('http://home.mobile.******')

versionInfo = response.text

第二步:解析 json # 引用 json 库用于解析 json 对象

import json

# versionInfo 是接口返回的 json 格式数据

# json.loads 将已编码的 JSON 字符串解码为 Python 对象

versionInfoPython = json.loads(versionInfo)

# print versionInfoPython

# 从字典里取 data 数组

dataList = versionInfoPython.get('data')

# print dataList

# 满足条件:platformJson 有 iOS 以及 status 为发布状态, 获得满足条件时相应的 version 值

# 取前20个遍历

firstDataList = dataList[0:20]

versionList = []

for x in firstDataList:

# print x['platformJson']

# 将 platformJson 对象解析为 platform, 由接口返回结果知 platformPython 是一个列表(数组)

platformPython = json.loads(x['platformJson'])

if len(platformPython) > 1 and x['status'] == 1 :

versionList.append(x['version'])

# print versionList

print versionList[0]

完整代码 #!/usr/bin/python

# -*- coding: utf-8 -*-

# 引用 json 库用于解析 json 对象

import json

# 使用 requests 库

import requests

response = requests.get('http://home.mobile.******')

versionInfo = response.text

# versionInfo 是接口返回的 json 格式数据

# json.loads 将已编码的 JSON 字符串解码为 Python 对象

versionInfoPython = json.loads(versionInfo)

# print versionInfoPython

# 从字典里取 data 数组

dataList = versionInfoPython.get('data')

# print dataList

# 取前20个遍历

firstDataList = dataList[0:20]

versionList = []

for x in firstDataList:

# print x['platformJson']

# 将 platformJson 对象解析为 platform, 由接口返回结果知 platformPython 是一个列表(数组)

platformPython = json.loads(x['platformJson'])

# 当 platformPython 数组大于1时,一定包含 ios

# status : 1 已发布,2 开发中,4 内灰中

if len(platformPython) > 1 and x['status'] == 1 :

versionList.append(x['version'])

# print versionList

print versionList[0]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值