مقدمة JSON

JSON: JavaScript Object Notation (مكتبة العلامات النصية للمسجل JavaScript).

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.

send 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.

receive 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.

store 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 Object Notation)
  • يعد تنسيق JSON نموذجًا خفيفًا للتبادل البياني للبيانات
  • يكون تنسيق JSON واضحًا وسهل الفهم
  • تنسيق JSON مستقل عن اللغة*

*

يستخدم JSON نسيج JavaScript، ولكن تنسيق JSON هو نص نظيف.

يمكن قراءة النص كبيانات واستخدامه بأي لغة برمجة.

قدم Douglas Crockford تنسيق JSON لأول مرة.

لماذا نستخدم JSON؟

بما أن تنسيق JSON هو نص فقط، يمكنه أن ينتقل بسهولة بين الخادم والتصفح، ويستخدم كتنسيق بيانات لأي لغة برمجة.

يقدم JavaScript وظائف مدمجة لتحويل سلاسل النصوص مكتوبة بتنسيق JSON إلى عناصر JavaScript الأصلية:

JSON.parse()

لذلك، إذا كنت تستلم بيانات بتنسيق JSON من الخادم، يمكنك استخدامها كما تستخدم أي عنصر من JavaScript.