1. 程式人生 > 其它 >Python:json和jsonpath的基本用法

Python:json和jsonpath的基本用法

演示:

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還有很多方法很強大,有興趣的可以深挖哦!

每天進步一點點!