مقدمة إلى JSON
- الصفحة السابقة مثال AJAX
- الصفحة التالية نحوية JSON
JSON: JavaScript Object Notation (JavaScript Object Notation).
JSON is a syntax for storing and exchanging data.
JSON is written in text using JavaScript object notation.
Exchange data
When data is exchanged between the browser and the server, these data must be text.
JSON is text, and we can convert any JavaScript object to JSON and then send the JSON to the server.
We can also convert any JSON received from the server to a JavaScript object.
In this way, we can handle data as JavaScript objects without complex parsing and translation.
Sending data
If your data is stored in a JavaScript object, you can convert the object to JSON and then send it to the server.
Example
var myObj = { name:"Bill Gates", age:62, city:"Seattle" }; var myJSON = JSON.stringify(myObj); window.location = "demo_json.php?x=" + myJSON;
You will learn more about it in the later chapters of this tutorial JSON.stringify()
knowledge of functions.
Receiving data
If you receive data in JSON format, you can convert it to a JavaScript object:
Example
var myJSON = '{ "name":"Bill Gates", "age":62, "city":"Seattle" }'; var myObj = JSON.parse(myJSON); document.getElementById("demo").innerHTML = myObj.name;
You will learn more about it in the later chapters of this tutorial JSON.parse()
knowledge of functions.
Storing data
When storing data, the data must be in a specific format, and no matter where you choose to store it, text is always one of the valid formats.
JSON makes it possible for JavaScript objects to be stored as text.
Example
Store data in local storage
// store data: myObj = { name:"Bill Gates", age:62, city:"Seattle" }; myJSON = JSON.stringify(myObj); localStorage.setItem("testJSON", myJSON); // receive data: text = localStorage.getItem("testJSON"); obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.name;
ما هو JSON؟
- JSON يشير إلى وسم علامة الجسم JavaScript (JavaScript Object Notation)
- يعد JSON تنسيق بيانات خفيف الوزن
- يملك JSON خاصية التوصيف الذاتي وسهولة الفهم
- JSON مستقل عن اللغة*
*
يستخدم JSON جملة JavaScript ولكن تنسيق JSON هو نص خالص.
يمكن قراءة النص كبيانات واستخدامه بأي لغة برمجة.
تم اقتراح تنسيق JSON أولاً من قبل Douglas Crockford.
لماذا تستخدم JSON؟
بما أن تنسيق JSON هو نص فقط، يمكنه بسهولة أن يتم نقل بين الخادم وال متصفح ويستخدم كتنسيق بيانات لأي لغة برمجة.
يقدم JavaScript وظائف مدمجة لتحويل النصوص المكتوبة بتنسيق JSON إلى جسم JavaScript الأصلي:
JSON.parse()
لذلك، إذا كنت تحصل على بيانات بتنسيق JSON من الخادم، فإنك يمكنك استخدامها مثل أي جسم JavaScript آخر.
- الصفحة السابقة مثال AJAX
- الصفحة التالية نحوية JSON