Answer the question
In order to leave comments, you need to log in
Can you explain how it is accessible what is json in php?
I read the definitions on the Internet and googled, but I can’t really understand why it is needed and what are the advantages of it?
Answer the question
In order to leave comments, you need to log in
If you need to transfer from one machine to another (or from one program to another) a string, you pass a string.
And if you need to transfer a data structure, for example, several ints, a couple of strings, an array with a nested array - here you can pack the entire data set in XML or JSON and transfer it, and on the other side it will unpack immediately into a ready-made structure.
JSON is more compact, XML is more powerful.
Both are supported in different programming languages out of the box.
This is if in a nutshell.
JSON is a special kind of string that can be parsed on any side. Through it, you can transfer data arrays. For example, from php to javascript.
PHP:
// передаем массив $arr на клиент в виде JSON
$arr = [
'name' => 'Петр'
];
echo json_encode ($arr);
// принимаем строку, парсим и получаем объект таким, каким он был на сервере в PHP
var r = JSON.parse ('{"name": "Петр"}');
alert (r.name);
// выведет "Петр"
console.log (r);
/*
Получится объект такого вида:
var r = {
'name': 'Петр'
};
*/
When you implement a client-server application, you will immediately understand :) I was also skeptical about this :) And as soon as I saw all the beauty of JSON, I scolded myself for bad thoughts in his direction.)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question