Python json thay thế giá trị bằng khóa

Thông tin được tìm nạp bằng yêu cầu JSONP, yêu cầu này chứa văn bản quảng cáo và liên kết đến hình ảnh quảng cáo. Tệp JSON và hình ảnh được tìm nạp từ buysellads. com hoặc buysellads. mạng lưới

Nếu bạn có bất kỳ câu hỏi hoặc thắc mắc nào, xin vui lòng gửi email

Giả sử bạn đã nhận được sinh viên sau, JSON. Và bạn muốn kiểm tra xem khóa phần trăm có hay không trong dữ liệu JSON. nếu nó hiện diện trực tiếp để truy cập giá trị của nó thay vì lặp lại toàn bộ JSON

student ={ 
   "id":1,
   "name":"john wick",
   "class":8,
   "percentage":75,
   "email":"[email protected]"
}

Thí dụ

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")

đầu ra

Checking if percentage key exists in JSON
Key exist in JSON data
john wick marks is:  75

Ghi chú. Chúng tôi đã sử dụng phương pháp

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
3 để chuyển đổi dữ liệu được mã hóa JSON thành từ điển Python. Sau khi biến dữ liệu JSON thành từ điển, chúng ta có thể kiểm tra xem khóa có tồn tại hay không

Kiểm tra xem có giá trị cho khóa trong JSON không

Chúng tôi cần một giá trị của khóa có trong JSON để chúng tôi có thể sử dụng giá trị này trong hệ thống của mình. Trong trường hợp này, chúng tôi cần đảm bảo rằng giá trị đó có cho một khóa và nếu không có hoặc không có, chúng tôi sẽ sử dụng giá trị mặc định

Ví dụ để kiểm tra xem có giá trị cho khóa trong JSON không

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": null,
   "percentage": 75,
   "email": "[email protected]"
}"""
student = json.loads(studentJson)
if not (student.get('email') is None):
     print("value is present for given JSON key")
     print(student.get('email'))
else:
    print("value is not present for given JSON key")

đầu ra

value is present for given JSON key
[email protected]

Trả về giá trị mặc định nếu khóa bị thiếu

Hãy xem cách sử dụng giá trị mặc định nếu giá trị đó không có trong khóa. Như các bạn đã biết, phương thức

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
4 convert dữ liệu JSON sang Python
import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
5 nên chúng ta có thể sử dụng phương thức get của lớp
import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
5 để gán giá trị mặc định cho key nếu giá trị đó bị thiếu

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": null,
   "percentage": 75,
   "email": "[email protected]"
}"""

student = json.loads(studentJson)
if not (student.get('subject') is None):
    print("value is present for given JSON key")
    print(student.get('subject'))
else:
    print("using a default value for a given key")
    print(student.get('subject', 'Science'))

đầu ra

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
0

Python Tìm nếu khóa lồng nhau tồn tại trong JSON

Hầu hết thời gian, JSON chứa rất nhiều khóa lồng nhau. Hãy xem cách truy cập trực tiếp các cặp khóa-giá trị lồng nhau từ JSON. Giả sử bạn có dữ liệu JSON sau. và bạn muốn kiểm tra và truy cập giá trị của các dấu khóa lồng nhau

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
1

ví dụ 1. Truy cập khóa lồng nhau trực tiếp

Nếu bạn biết rằng bạn luôn có khóa cha, thì bạn có thể truy cập trực tiếp vào khóa JSON lồng nhau. Trong trường hợp này, chúng ta luôn có khóa ‘class‘ và ‘student‘ để có thể truy cập trực tiếp vào các dấu khóa lồng nhau

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
2

đầu ra

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
3

ví dụ 2. Truy cập khóa lồng nhau bằng cách sử dụng câu lệnh

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
7 lồng nhau

Nếu bạn không chắc liệu mình có luôn có khóa cha hay không, trong những trường hợp như vậy, chúng ta cần truy cập JSON lồng nhau bằng cách sử dụng câu lệnh if lồng nhau, để tránh bất kỳ trường hợp ngoại lệ nào

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
0

Lặp lại mảng JSON

Nhiều lần khóa JSON lồng nhau chứa một giá trị ở dạng mảng hoặc từ điển. Trong trường hợp này, nếu bạn cần tất cả các giá trị, chúng ta có thể lặp lại mảng JSON lồng nhau. Hãy xem ví dụ

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
1

đầu ra

import json

studentJson ="""{
   "id": 1,
   "name": "john wick",
   "class": 8,
   "percentage": 75,
   "email": "[email protected]"
}"""

print("Checking if percentage key exists in JSON")
student = json.loads(studentJson)
if "percentage" in student:
    print("Key exist in JSON data")
    print(student["name"], "marks is: ", student["percentage"])
else:
    print("Key doesn't exist in JSON data")
2

Vậy bạn nghĩ như thế nào?

Tôi muốn nghe từ bạn. Bạn nghĩ gì về bài viết này?

Ngoài ra, hãy thử giải Bài tập JSON của Python để hiểu rõ hơn về Làm việc với Dữ liệu JSON trong Python