演示:
data={ "code": 0, "message": "ok", "data": { "total": "39", "limit": "10", "offset": "0", "list": [ { "id": "108100000074", "villageName": "testvillage+1639619515", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1639619580560", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1200" ] }, { "id": "108100000063", "villageName": "57c9eca4-f1ba-486a-a7ad-5c68b02fc5ea", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497742685", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000062", "villageName": "6f74e025-d789-4054-b600-dcc3caeaad09", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497735595", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000061", "villageName": "d2eac1b8-7722-4beb-bf06-fe3d65f867c0", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497730588", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000060", "villageName": "4fcae573-261b-4ee5-bebf-b97b0d00b009", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497721104", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000059", "villageName": "a35a299e-c803-41b4-8b63-eec5df423fb3", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497718790", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000058", "villageName": "b96f4d17-6eb8-4e20-85f4-d32c8ae9de1e", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497715207", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000057", "villageName": "9fa89219-6bc5-410f-bdd1-5ed3da419245", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497711380", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000056", "villageName": "fec8d8e8-5e85-463f-b979-7b6b833e807d", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497708276", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] }, { "id": "108100000055", "villageName": "6573c4a1-0283-48eb-8d4e-531ca879de4c", "address": "test_address", "associateOrganization": { "organizationId": "100110000001", "organizationName": "社区_5.0.5" }, "createTime": "1638497700649", "placeId": "1204201110800000024", "polyline": "", "operater": "101003000001", "createName": "殷千君", "thirdIsDeleteFlag": false, "divisionsCodes": [ "1204200000000000", "1204201000000000", "1204201110000000", "1204201110800000" ] } ], "other": {} } }
import jsonpath,json
#这里的data目前是字典类型,记住一点外层加了引号的是字符串,通俗说json相当于字典形式的字符串
#将data转化成json字符串
data1=json.dumps(data)
#再查看data1的数据类型
print(type(data1)) #输出结果是<class 'str'>
#如果原始数据data外层加了引号,那就是json字符串,转成字典的话用json.loads(data)
#***************************************************************************************#
#jsonpath的基本用法和场景,实际工作中接口往往返回的数据是json格式,里面数据多,层级深,现在需要获取data数据中所有的id,应该怎么做?
#方法一:之前常用的办法是通过字典的方式获取数据中“list”这个列表,然后for循环获取键值并追加到列表中,最后输出列表
data2=data["data"]["list"]
ids1=[]
for id in data2:
ids1.append(id["id"])
print(ids1) #输出结果['108100000074', '108100000063', '108100000062', '108100000061', '108100000060', '108100000059', '108100000058', '108100000057', '108100000056', '108100000055']
#方法二:使用jsonpath获取所有id
ids2=jsonpath.jsonpath(data,"$..id") #匹配数据里面所有的id
ids3=jsonpath.jsonpath(data,"$.data.list[*].id") #匹配list下面的所有的id
print(ids2)
print(ids3) #ids2和ids3的输出结果都是['108100000074', '108100000063', '108100000062', '108100000061', '108100000060', '108100000059', '108100000058', '108100000057', '108100000056', '108100000055']
大家学“废“”了吗?jsonpath还有很多方法很强大,有兴趣的可以深挖哦!