เพื่อแปลง JSON ใช้ parseJSON() และสำหรับแสดงคู่เฉพาะ ใช้ฟังก์ชัน $.each()
สมมติ JSON ของเราที่แปลงแล้วเป็นดังนี้
const APIData = '[{"Name":"John","Age":21},{"Name":"David","Age":24},{"Name":"Bob","Age":20}]'; const getObject = jQuery.parseJSON(APIData);
ตอนนี้มาดูโค้ดเต็มและดึงคู่ "Name" กัน
ตัวอย่าง
Document
เพื่อรันโปรแกรมด้านบน ให้บันทึกชื่อไฟล์ "anyName.html(index.html)" และคลิกขวาที่ไฟล์ เลือก "Open with Live Server" ใน VS Code editor
ผลลัพธ์
จะได้ผลลัพธ์ดังนี้
