栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

云学编程的21天—【微软官方python入门教程 P38-P39笔记】2021-11-21 P38-P39 JSON的三种结构

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

云学编程的21天—【微软官方python入门教程 P38-P39笔记】2021-11-21 P38-P39 JSON的三种结构

Many web services return data as JSON

 

 

JSON is a standard data format that can be intimidating at first glance.

Using a linting tool to format JSON makes it easier to read

JSON contains key pairs    

"kay":"value"  

{"kay":{"subkey0":"subvalue0","subkey1":"subvalue1,....."}}

{"key":[listvalue0,listvalue1,listvalue2,...]}

Basically what you'll notice is there's a pattern to how the JSON data looks.

To retrieve the value from a "key":"value" request the key name

print(results['requestId'])

To request a value from a {"kay":{"subkey0":"subvalue0","subkey1":"subvalue1,....."}} specify the key name and the subkey name

print(results['color']['dominantcolorbackground'])

To request a value from a {"kay":{"subkey0":"subvalue0","subkey1":"subvalue1,....."}} specify the keyname and index position of the value to retrieve

print(results['description']['tags'][0])

Use a loop to retreve all values from a {"key":[listvalue0,listvalue1,listvalue2,...]}

for item in results['description']['tags']:
    print(item)

You can create "key":"value"JSON objects from a dictionary

#Create a dictionary object
person_dict = {'first':'Christopher','last':'Harrison'}
#Add additional key pairs as needed to dictionary
person_dict['City']='Seattle'

#Convert dictionary to JSON object
person_json = json.dumps(person_dict)
print(person_json)

注意先要import json 

P39实操:

​​​​​​​她视频里给的地址没权限,有时间自己找一个试试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/580725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号