JSON logo |
A few days ago at work, my boss ordered me some programming tasks. I was very happy. As I told some time ago that was not programmed because I had been assigned other tasks. Taken at a junior and send it to run errands. So I will never move from junior :)
But complaints aside, I'm to the point. It turns out that I was commissioned to make a PHP script that generates a json file to read it from javascript.
JSON
I like to say "jota son", but people usually say "yei son". Is a data representation format that encapsulates objects, arrays or variables in a special notation.
JSON is JavaScript Object Notation. And basically it, the way in which objects are represented in Javascript. Inside it may be from a single variable, an array of whole objects, etc.
JSON vs XML
XML vs JSON |
XML is another way of representing data, but uses tags to format it like HTML. The advantage of JSON is basically two:
- It's lighter than XML, because it does not add labels to add many bytes to content.
- You can read almost directly in JavaScript and that makes it faster. In the case of XML is that process more and that makes it slower.
AJAX
In recent years Ajax has become fashionable. How you communicate with the server at first was the XML. But the burst JSON to be imposed for those benefits I mentioned earlier. So many communications are made in Json ajax, many web services now use only this format.
What they asked me
What they asked me to do, basically, is a kind of web service.
From a web page, using ajax, are requested data to a url (where hosts my PHP script). These data are used to build a table, which is not important at this time what is its purpose.
On the server a file is included in my php script that contains a variable with data. This variable is populated with data from the database at a certain period of time (that I am not concerned me, because it is already done.)
This variable is an object that contains an associative array with values, such as:
![]() |
| Ajax Team |
$data = array(
'dato1' => 1,
'dato2' => 2,
'dato3' => 3,
'dato4' => 4,
'dato5' => 5,
);
After searching the web for a while, and that some of my colleagues at work told me I had to include a library to do so, I found that it brings a PHP function to do this work:
- json_encode, which returns a string in JSON format to the representation of what we as a parameter.
and also is its counterpart:
- json_decode to decode the json that we passed as a parameter and returns the variable, array or object it represents.
json_encode()
But in my case I just needed json_encode. So I just had to do this:
PHP Elephant |
$json = json_encode($data);
and then simply print.
echo $json;
and it prints the following:
{"dato1":1,"dato2":2,"dato3":3,"dato4":4,"dato5":5}
JSON decoding
I did not need it, but if you send me the json to get the equivalent in PHP:
$json = '{"dato1":1,"dato2":2,"dato3":3,"dato4":4,"dato5":5}';
$data = json_decode($json);
And ready in $data as well as we had before. So easy.
Every so often you have to read the PHP manual
This feature is available from version 5.2. It seems that once used a library for JSON objects, or was done by hand, so my colleagues advised me that. But I searched the PHP manual and found these functions.
Sometimes you have to search in the php manual. Is a language that is constantly evolving and may be that what we had to do by hand, now is fixed in a single native function :)
Hopefully it helps someone :)
Diego Arregui






No comments:
Post a Comment
No abuse please! :)