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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การถอดรหัสและเข้ารหัส JSON เบื้องต้นด้วยภาษา Python

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

โดย บุคคลทั่วไป » 15/02/2022 2:40 am

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

โดย บุคคลทั่วไป » 24/01/2022 1:25 am

"name": "\u00e0\u00b8\u00aa\u00e0\u00b8\u00b4\u00e0\u00b8\u00a3\u00e0\u00b8\u00b4\u00e0\u00b8\u00a8\u00e0\u00b8\u00b1\u00e0\u00b8\u0081\u00e0\u00b8\u0094\u00e0\u00b8\u00b4\u00e0\u00b9\u008c \u00e0\u00b8\u00a8\u00e0\u00b8\u00a3\u00e0\u00b8\u00b5\u00e0\u00b8\u00a7\u00e0\u00b8\u00b4\u00e0\u00b8\u009e\u00e0\u00b8\u00b1\u00e0\u00b8\u0092\u00e0\u00b8\u0099\u00e0\u00b9\u008c"
},
{

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

โดย JSON » 15/01/2022 7:58 am

ขอบคุณค่ะ กำลังหาข้อมูลอยู่

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

โดย บุคคลทั่วไป » 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

โดย บุคคลทั่วไป » 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
},

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

โดย 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,

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

โดย บุคคลทั่วไป » 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"
}

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

โดย Naraporn Benz » 09/03/2019 11:14 am

"\ u00e0 \ u00b8 \ u0096 \ u00e0 \ u00b9 \ u0089 \ u00e0 \ u00b8 \ u00b2 \ u00e0 \ u00b8 \ u00a1 \ u00e0 \ u00b8 \ u00b1 \ u00e0 \ u00b8 \ u0099 \ u00e0 \ u00b9 \ u0082 \ u00e0 \ u00b8 \ u0097 \ u00e0 \ u00b8 \ u00a3 \ u00e0 \ u00b9 \ u0084 \ u00e0 \ u00b8 \ u009b \ u00e0 \ u00b8 \ u0097 \ u00e0 \ u00b8 \ u00b1 \ u00e0 \ u00b8 \ u0081 \ u00e0 \ u00b8 \ u00a1 \ u00e0 \ u00b8 \ u00b2 \ u00e0 \ u00b8 \ u009a \ u00e0 \ u00b8 \ u00ad \ u00e0 \ u00b8 \ u0081 \ u00e0 \ u00b8 \ u0094 \ u00e0 \ u00b9 \ u0089 \ u00e0 \ u00b8 \ u00a7 \ u00e0 \ u00b8 \ u00a2 \ u00e0 \ u00b8 \ u0099 \ u00e0 \ u00b8 \ u00b0 "

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

โดย kritsadak » 28/12/2018 2:42 pm

ในบทความนี้ จะเป็นการแนะนำการเข้ารหัสข้อมูล และการถอดรหัสรูปแบบของ 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']+' ของโปรแกรมหลายภาษา รวมทั้งให้คำปรึกษาหรือสอบถามข้อมูลได้')
ผลลัพท์ที่ได้
รูปภาพ

ข้างบน