การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

kritsadak
PHP Full Member
PHP Full Member
โพสต์: 30
ลงทะเบียนเมื่อ: 24/12/2018 10:18 am

การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย kritsadak »

ในบทความนี้ จะเป็นการแนะนำการเข้ารหัสข้อมูล และการถอดรหัสรูปแบบของ Json หรือ Java Script Object Notation เพื่ออ่านค่าภายในข้อมูลนั้นๆ หรือเพื่อดึงค่าที่ต้องการนั้นออกมาด้วยภาษา Python

ตัวอย่าง ข้อมูลที่ยังไม่ถูกเข้ารหัสข้อมูลเป็น Json
{
"FirstName":"MindPHP",
"LastName":"com",
"NickName":"Knowledge หรือความรู้"
}
ขั้นแรก เริ่มต้นด้วยการเพิ่ม library json ให้กับ python

โค้ด: เลือกทั้งหมด

import json
ในส่วนของการ Dumps (เข้ารหัสข้อมูล)

ตัวอย่าง Code ทั้งหมดของการ dumps ข้อมูลให้เป็น Json

โค้ด: เลือกทั้งหมด

#! /usr/bin/python3
# -*- coding: utf-8 -*-
import json
detail = {
    "FirstName":"MindPHP",
    "LastName":"com",
    "NickName":"Knowledge หรือความรู้"
}
dumping = json.dumps(detail) 
print(dumping)
d่umps เป็น function ที่ต้องการใช้ในภาษา Python ทีนี้เราก็จะเขียนคำสั่งนี้ลงไป

โค้ด: เลือกทั้งหมด

json.dumps(ข้อมูลที่ต้องการเข้ารหัส)
** หมายเหตุ แล้วถ้ารหัสของข้อมูลมีภาษาไทยรวมอยู่ด้วย ผลลัพท์จะเป็น
รูปภาพ


ดังนั้น วิธีแก้ไข หากว่าข้อมูลนั้นเป็น ภาษาไทย ก็ต้องเพิ่มเติมคำสั่งนี้ลงไปด้วยเสมอ เพื่อแปลงข้อมูล

โค้ด: เลือกทั้งหมด

json.dumps(ข้อมูลที่ต้องการเข้ารหัส, ensure_ascii=False)
ผลลัพท์ก็จะได้
รูปภาพ


ในส่วนของการ Loads (ถอดรหัสข้อมูล)

จากนั้น เมื่อเราได้ข้อมูลที่ถูกเข้ารหัสออกมาเป็น json แล้ว เราก็จะทำการถอดรหัสข้อมูล เพื่อดึงค่าภายในของข้อมูลนั้นออกมา และสามารถดึงข้อมูลออกมาแสดงได้โดยใช้คำสั่งนี้

โค้ด: เลือกทั้งหมด

json.loads(ข้อมูลที่ต้องการถอดรหัส)

print(ชื่อObject['ข้อมูลที่ต้องการจะดึงข้อมูลออกมาแสดง'])
ตัวอย่าง Code ทั้งหมด dumps และ loads

โค้ด: เลือกทั้งหมด

#! /usr/bin/python3
# -*- coding: utf-8 -*-
import json
detail = {
    "FirstName":"MindPHP",
    "LastName":"com",
    "NickName":"Knowledge หรือความรู้"
}
# -- dumps
dumping = json.dumps(detail) 
#print(dumping)


# -- loads
loading = json.loads(json_detail)
print(loading['FirstName'])
print(loading['LastName'])
print(loading['NickName'])
print()
print('www.'+loading['FirstName']+'.'+loading['LastName']+' เป็นเว็ปไซต์ที่ให้ '+loading['NickName']+' ของโปรแกรมหลายภาษา รวมทั้งให้คำปรึกษาหรือสอบถามข้อมูลได้')
ผลลัพท์ที่ได้
รูปภาพ
บุคคลทั่วไป

Re: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

name": "วิวัฒน์สุขดี"
},
{
"name": "ศิริรัตน์ศิริรัตน์"
}
],
"ข้อความ": [
{
"sender_name": "วิวัฒน์สุขดี",
"timestamp_ms": 1525008355497,
"เนื้อหา": "\ u00e0 \ u00b8 \ u0084 \ u00e0 \ u00b8 \ u00b8 \ u00e0 \ u00b8 \ u0093 \ u00e0 \ u00b9 \ u0080 \ u00e0 \ u00b8 \ u008a \ u00e0 \ u00b8 \ u00b7 \ u00e0 \ u00b8 \ u00b7 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u00a1 \ u00e0 \ u00b8 \ u0095 \ u00e0 \ u00b9 \ u0088 \ u00e0 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u0081 \ u00e0 \ u00b8 \ u00b1 \ u00b8 \ u00b1 u009a \ u00e0 \ u00b8 \ u0099 Messenger \ u00e0 \ u00b9 \ u0081 \ u00e0 \ u00b8 \ u00a5 \ u00e0 \ u00b9 \ u0089 \ u00e0 \ u00b8 \ u00a7 \ u00e0 \ u00b9 \ u0083 \ u00e0 \ u00b9 \ u0083 \ u00e0 \ u00e0 \ u00b8 \ u0093 \ u00e0 \ u00b8 \ u00b0 \ u00e0 \ u00b8 \ u0099 \ u00e0 \ u00b8 \ u00b5 \ u00e0 \ u00b9 \ u0089 ",
"type": "ทั่วไป"
}
],
"title": "วิวัฒน์สุขดี",
"is_still_participant": จริง
"thread_type": "ปกติ",
"thread_path": "archived_threads / wivadsukdee_m4tej2fy6w"
}
Icenatcha

ต้องการเเปลเนื้อหาทั้งหมด

โพสต์ที่ยังไม่ได้อ่าน โดย Icenatcha »

{
"participants": [
{
"name": "Ruethaichanok Ploiprasert"
}, {
"name": "Benz Gk" }
], "messages": [
{
"sender_name": "Benz Gk", "timestamp_ms": 1591804676287, "content":
"\u00e0\u00b8\u0084\u00e0\u00b8\u00a3\u00e0\u00b8\u00b1\u00e0\u00b8\ u009a",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804671149, "content":
"\u00e0\u00b9\u0082\u00e0\u00b8\u00ad\u00e0\u00b8\u0099\u00e0\u00b9 \u0084\u00e0\u00b8\u009b\u00e0\u00b8\u00a5\u00e0\u00b8\u00b0\u00e0\ u00b8\u0099\u00e0\u00b8\u00b0",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804668510, "content":
"\u00e0\u00b8\u0084\u00e0\u00b9\u0089\u00e0\u00b8\u00b2\u00e0\u00b8 \u009a",
"type": "Generic" },
{
"sender_name": "Benz Gk", "timestamp_ms": 1591804613481, "content":
"\u00e0\u00b9\u0084\u00e0\u00b8\u0094\u00e0\u00b9\u0089\u00e0\u00b8 \u0084\u00e0\u00b8\u00b1\u00e0\u00b8\u009a",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804601071,
บุคคลทั่วไป

Re: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

"sender_name": "S'aranporn Rungrat",
"timestamp_ms": 1635605155484,
"content": "\u00e0\u00b8\u00ad\u00e0\u00b9\u0088\u00e0\u00b8\u00ad\u00e0\u00b9\u0080\u00e0\u00b8\u0084\u00e0\u00b8\u00a3",
"type": "Generic",
"is_unsent": false
},
บุคคลทั่วไป

Re: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

Guest เขียน: 14/12/2020 10:25 pm name": "วิวัฒน์สุขดี"
},
{
"name": "ศิริรัตน์ศิริรัตน์"
}
],
"ข้อความ": [
{
"sender_name": "วิวัฒน์สุขดี",
"timestamp_ms": 1525008355497,
"เนื้อหา": "\ u00e0 \ u00b8 \ u0084 \ u00e0 \ u00b8 \ u00b8 \ u00e0 \ u00b8 \ u0093 \ u00e0 \ u00b9 \ u0080 \ u00e0 \ u00b8 \ u008a \ u00e0 \ u00b8 \ u00b7 \ u00e0 \ u00b8 \ u00b7 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u00a1 \ u00e0 \ u00b8 \ u0095 \ u00e0 \ u00b9 \ u0088 \ u00e0 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u0081 \ u00e0 \ u00b8 \ u00b1 \ u00b8 \ u00b1 u009a \ u00e0 \ u00b8 \ u0099 Messenger \ u00e0 \ u00b9 \ u0081 \ u00e0 \ u00b8 \ u00a5 \ u00e0 \ u00b9 \ u0089 \ u00e0 \ u00b8 \ u00a7 \ u00e0 \ u00b9 \ u0083 \ u00e0 \ u00b9 \ u0083 \ u00e0 \ u00e0 \ u00b8 \ u0093 \ u00e0 \ u00b8 \ u00b0 \ u00e0 \ u00b8 \ u0099 \ u00e0 \ u00b8 \ u00b5 \ u00e0 \ u00b9 \ u0089 ",
"type": "ทั่วไป"
}
],
"title": "วิวัฒน์สุขดี",
"is_still_participant": จริง
"thread_type": "ปกติ",
"thread_path": "archived_threads / wivadsukdee_m4tej2fy6w"
}
Icenatcha เขียน: 26/12/2020 1:06 pm {
"participants": [
{
"name": "Ruethaichanok Ploiprasert"
}, {
"name": "Benz Gk" }
], "messages": [
{
"sender_name": "Benz Gk", "timestamp_ms": 1591804676287, "content":
"\u00e0\u00b8\u0084\u00e0\u00b8\u00a3\u00e0\u00b8\u00b1\u00e0\u00b8\ u009a",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804671149, "content":
"\u00e0\u00b9\u0082\u00e0\u00b8\u00ad\u00e0\u00b8\u0099\u00e0\u00b9 \u0084\u00e0\u00b8\u009b\u00e0\u00b8\u00a5\u00e0\u00b8\u00b0\u00e0\ u00b8\u0099\u00e0\u00b8\u00b0",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804668510, "content":
"\u00e0\u00b8\u0084\u00e0\u00b9\u0089\u00e0\u00b8\u00b2\u00e0\u00b8 \u009a",
"type": "Generic" },
{
"sender_name": "Benz Gk", "timestamp_ms": 1591804613481, "content":
"\u00e0\u00b9\u0084\u00e0\u00b8\u0094\u00e0\u00b9\u0089\u00e0\u00b8 \u0084\u00e0\u00b8\u00b1\u00e0\u00b8\u009a",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804601071,
Guest เขียน: 04/11/2021 11:16 am "sender_name": "S'aranporn Rungrat",
"timestamp_ms": 1635605155484,
"content": "\u00e0\u00b8\u00ad\u00e0\u00b9\u0088\u00e0\u00b8\u00ad\u00e0\u00b9\u0080\u00e0\u00b8\u0084\u00e0\u00b8\u00a3",
"type": "Generic",
"is_unsent": false
},
"messages": [
{
"sender_name": "S'aranporn Rungrat",
"timestamp_ms": 1635605155484,
"content": "\u00e0\u00b8\u00ad\u00e0\u00b9\u0088\u00e0\u00b8\u00ad\u00e0\u00b9\u0080\u00e0\u00b8\u0084\u00e0\u00b8\u00a3",
"type": "Generic",
"is_unsent": false
},
บุคคลทั่วไป

Re: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

Guest เขียน: 07/11/2021 12:41 am
Guest เขียน: 14/12/2020 10:25 pm name": "วิวัฒน์สุขดี"
},
{
"name": "ศิริรัตน์ศิริรัตน์"
}
],
"ข้อความ": [
{
"sender_name": "วิวัฒน์สุขดี",
"timestamp_ms": 1525008355497,
"เนื้อหา": "\ u00e0 \ u00b8 \ u0084 \ u00e0 \ u00b8 \ u00b8 \ u00e0 \ u00b8 \ u0093 \ u00e0 \ u00b9 \ u0080 \ u00e0 \ u00b8 \ u008a \ u00e0 \ u00b8 \ u00b7 \ u00e0 \ u00b8 \ u00b7 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u00a1 \ u00e0 \ u00b8 \ u0095 \ u00e0 \ u00b9 \ u0088 \ u00e0 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u0081 \ u00e0 \ u00b8 \ u00b1 \ u00b8 \ u00b1 u009a \ u00e0 \ u00b8 \ u0099 Messenger \ u00e0 \ u00b9 \ u0081 \ u00e0 \ u00b8 \ u00a5 \ u00e0 \ u00b9 \ u0089 \ u00e0 \ u00b8 \ u00a7 \ u00e0 \ u00b9 \ u0083 \ u00e0 \ u00b9 \ u0083 \ u00e0 \ u00e0 \ u00b8 \ u0093 \ u00e0 \ u00b8 \ u00b0 \ u00e0 \ u00b8 \ u0099 \ u00e0 \ u00b8 \ u00b5 \ u00e0 \ u00b9 \ u0089 ",
"type": "ทั่วไป"
}
],
"title": "วิวัฒน์สุขดี",
"is_still_participant": จริง
"thread_type": "ปกติ",
"thread_path": "archived_threads / wivadsukdee_m4tej2fy6w"
}
Icenatcha เขียน: 26/12/2020 1:06 pm {
"participants": [
{
"name": "Ruethaichanok Ploiprasert"
}, {
"name": "Benz Gk" }
], "messages": [
{
"sender_name": "Benz Gk", "timestamp_ms": 1591804676287, "content":
"\u00e0\u00b8\u0084\u00e0\u00b8\u00a3\u00e0\u00b8\u00b1\u00e0\u00b8\ u009a",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804671149, "content":
"\u00e0\u00b9\u0082\u00e0\u00b8\u00ad\u00e0\u00b8\u0099\u00e0\u00b9 \u0084\u00e0\u00b8\u009b\u00e0\u00b8\u00a5\u00e0\u00b8\u00b0\u00e0\ u00b8\u0099\u00e0\u00b8\u00b0",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804668510, "content":
"\u00e0\u00b8\u0084\u00e0\u00b9\u0089\u00e0\u00b8\u00b2\u00e0\u00b8 \u009a",
"type": "Generic" },
{
"sender_name": "Benz Gk", "timestamp_ms": 1591804613481, "content":
"\u00e0\u00b9\u0084\u00e0\u00b8\u0094\u00e0\u00b9\u0089\u00e0\u00b8 \u0084\u00e0\u00b8\u00b1\u00e0\u00b8\u009a",
"type": "Generic" },
{
"sender_name": "Ruethaichanok Ploiprasert", "timestamp_ms": 1591804601071,
Guest เขียน: 04/11/2021 11:16 am "sender_name": "S'aranporn Rungrat",
"timestamp_ms": 1635605155484,
"content": "\u00e0\u00b8\u00ad\u00e0\u00b9\u0088\u00e0\u00b8\u00ad\u00e0\u00b9\u0080\u00e0\u00b8\u0084\u00e0\u00b8\u00a3",
"type": "Generic",
"is_unsent": false
},
"messages": [
{
"sender_name": "S'aranporn Rungrat",
"timestamp_ms": 1635605155484,
"content": "\u00e0\u00b8\u00ad\u00e0\u00b9\u0088\u00e0\u00b8\u00ad\u00e0\u00b9\u0080\u00e0\u00b8\u0084\u00e0\u00b8\u00a3",
"type": "Generic",
"is_unsent": false
},
บุคคลทั่วไป

Re: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

การใช้งาน JSON ใน Python

Python มีไลบรารีชื่อ json ที่ช่วยให้เราสามารถทำงานกับ JSON ได้ง่าย ๆ เราสามารถใช้ไลบรารีนี้เพื่อเข้ารหัส (encode) และถอดรหัส (decode) ข้อมูล JSON ได้

การถอดรหัส JSON (Decoding)
การถอดรหัส JSON หมายถึงการแปลงข้อมูล JSON ให้เป็นข้อมูลที่เราสามารถใช้งานได้ใน Python เช่น การแปลง JSON เป็นดิกชันนารี (dictionary) หรือรายการ (list)

โค้ด: เลือกทั้งหมด

import json

# ข้อมูล JSON เป็นสตริง (string)
json_data = '{"name": "Alice", "age": 30, "city": "Wonderland"}'

# ถอดรหัส JSON เป็นดิกชันนารีของ Python
python_data = json.loads(json_data)

print(python_data)
print(python_data['name'])  # เข้าถึงค่าภายในดิกชันนารี
ในตัวอย่างนี้:
  • เรานำเข้ามอดูล json
  • ใช้ json.loads เพื่อแปลงสตริง JSON เป็นดิกชันนารีของ Python
  • จากนั้นเราสามารถใช้งานดิกชันนารีนี้เหมือนดิกชันนารีปกติใน Python
การเข้ารหัส JSON (Encoding)
การเข้ารหัส JSON หมายถึงการแปลงข้อมูลใน Python ให้เป็น JSON เช่น การแปลงดิกชันนารีหรือรายการใน Python ให้เป็นสตริง JSON

โค้ด: เลือกทั้งหมด

import json

# ข้อมูลเป็นดิกชันนารีของ Python
python_data = {
    "name": "Bob",
    "age": 25,
    "city": "Builderland"
}

# เข้ารหัสดิกชันนารีเป็น JSON
json_data = json.dumps(python_data)

print(json_data)
- เราใช้ json.dumps เพื่อแปลงดิกชันนารีของ Python เป็นสตริง JSON
- จากนั้นเราสามารถส่งสตริง JSON นี้ไปยังที่อื่นได้ เช่น การส่งข้อมูลผ่าน API หรือการเก็บข้อมูลในไฟล์

แถมๆ
การอ่านและเขียนไฟล์ JSON
บางครั้งเราอาจต้องการอ่านข้อมูล JSON จากไฟล์ หรือเขียนข้อมูล JSON ลงในไฟล์

ตัวอย่างการอ่านไฟล์ JSON

โค้ด: เลือกทั้งหมด

import json

# อ่านข้อมูล JSON จากไฟล์
with open('data.json', 'r') as file:
    python_data = json.load(file)

print(python_data)
ตัวอย่างการเขียนไฟล์ JSON

โค้ด: เลือกทั้งหมด

import json

# ข้อมูลเป็นดิกชันนารีของ Python
python_data = {
    "name": "Charlie",
    "age": 35,
    "city": "Charlestown"
}

# เขียนข้อมูล JSON ลงในไฟล์
with open('data.json', 'w') as file:
    json.dump(python_data, file)
ในตัวอย่างนี้:
เราใช้ json.load เพื่ออ่านข้อมูล JSON จากไฟล์และแปลงเป็นดิกชันนารีของ Python
เราใช้ json.dump เพื่อเขียนดิกชันนารีของ Python ลงในไฟล์ในรูปแบบ JSON
ภาพประจำตัวสมาชิก
0961454478
PHP Newbie
PHP Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 17/08/2024 2:49 am

Re: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

โพสต์ที่ยังไม่ได้อ่าน โดย 0961454478 »

{
"settings_upgraded_to_cross_app_messaging": [
{
"title": "",
"media_map_data": {

},
"string_map_data": {
"\u00e0\u00b8\u00ad\u00e0\u00b8\u00b1\u00e0\u00b8\u009e\u00e0\u00b9\u0080\u00e0\u00b8\u0081\u00e0\u00b8\u00a3\u00e0\u00b8\u0094\u00e0\u00b9\u0080\u00e0\u00b8\u009b\u00e0\u00b9\u0087\u00e0\u00b8\u0099\u00e0\u00b8\u0081\u00e0\u00b8\u00b2\u00e0\u00b8\u00a3\u00e0\u00b8\u00aa\u00e0\u00b9\u0088\u00e0\u00b8\u0087\u00e0\u00b8\u0082\u00e0\u00b9\u0089\u00e0\u00b8\u00ad\u00e0\u00b8\u0084\u00e0\u00b8\u00a7\u00e0\u00b8\u00b2\u00e0\u00b8\u00a1\u00e0\u00b8\u0082\u00e0\u00b9\u0089\u00e0\u00b8\u00b2\u00e0\u00b8\u00a1\u00e0\u00b9\u0081\u00e0\u00b8\u00ad\u00e0\u00b8\u009e\u00e0\u00b9\u0081\u00e0\u00b8\u00a5\u00e0\u00b9\u0089\u00e0\u00b8\u00a7": {
"href": "",
"value": "False",
"timestamp": 0
},
"\u00e0\u00b9\u0080\u00e0\u00b8\u00a7\u00e0\u00b8\u00a5\u00e0\u00b8\u00b2\u00e0\u00b8\u0097\u00e0\u00b8\u00b5\u00e0\u00b9\u0088\u00e0\u00b8\u00ad\u00e0\u00b8\u00b1\u00e0\u00b8\u009e\u00e0\u00b9\u0080\u00e0\u00b8\u0081\u00e0\u00b8\u00a3\u00e0\u00b8\u0094": {
"href": "",
"value": "",
"timestamp": 0
}
}
}
]
}
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 10