Login or create a free accountShare your thoughts!
indiska Photo
Cristopher Cutas (Site Newb) wrote Sep 10, 2009
NICE!!

@Joel, Frank, Andrew..

THANK YOU!! im on php 5.2.6 so im good to go with JSON in the server side. its on the client where i want to generate my JSON. so eval() would be my friend and ajax for sending them to the server then decode would be my team mates friend. thank you all ..

@Frank, nice downloadable files, its not epsum epsum dolor something.

::editted my response, first response was weak.
Andrew Photo
Andrew Johnson (ITNewb Guru) wrote Sep 9, 2009
Nice work guys!

Hey Cris, here is a quick example to compliment the info Joel and Frank provided.

Unless your JavaScript script is fetching a JSON object from a server via AJAX, you have to manually build JSON objects as Frank described on page 1 in Intro to JSON.

If you directly build the JSON object in JavaScript, it's an object, not a string. If you fetch a JSON object generated by a server via AJAX, it will return a JSON encoded string that's not yet an active object. Once you fetch the JSON data from a server, you have to use JavaScript eval which will interpret the string as JavaScript. So if you assign that to a variable, the variable will become an active JSON object that you can access with dot notation as Frank described.

Here is a simple example:

  1. <?php
  2.  
  3. // array fetched from database
  4. $data = array(
  5.     'first' => 'Cris',
  6.     'last' => 'Cutas'
  7. );
  8.  
  9. // create json encoded form
  10. $jsonData = json_encode($data);
  11.  
  12. // json HTTP header as Joel mentioned
  13. header("Content-Type: application/json");
  14. // any HTTP cache related headers you need
  15. header("Cache-Control: no-cache");
  16. header("Pragma: no-cache");
  17. // send json string to client
  18. echo $jsonData;
  19.  
  20. ?>


Then in JavaScript:

  1. // note, jsonResponse here is the equivalent of httpRequest.responseText;
  2. function handleJSONResponse(jsonResponse) {
  3.     var jsonObject = eval( '(' + jsonResponse + ')' );
  4.     alert(jsonObject.first); // alerts "Cris"
  5. }


You may also want to read this article on wikipedia. Cheers!
Frank Photo
Frank Lakatos (Up and Comer) wrote Sep 9, 2009
Really good point Joel. Also, for simple json encoding, PHP 5.2 introduced json_encode() and json_decode(), respectively, that will convert associative arrays into JSON. Wrapper classes for most languages can be found at http://json.org/.
joelvardy Photo
Joel Vardy (Site Newb) wrote Sep 9, 2009
@indiska
On page three under the "Getting Started" header there is some information about generating json with php.. It is relitivly easy to manipulate the code to display dynamic content, for instance:
  1. "created_at" => date('r')

You may also want / need to set a header which will tell applications that this is JSON, you can do that in php with:
  1. header('Content-type: application/json');

Hope this helps, Joel
indiska Photo
Cristopher Cutas (Site Newb) wrote Sep 9, 2009
some how it feels lacking.. :| i think its just me, a nooby.. i want to know, if you can generate json or do you have to manually write the data..
MrPifPaf Photo
Guillaume Denormandie (Up and Comer) wrote May 13, 2009
Good work Frank !

A+ :)
neeko Photo
Phil Dufault (Up and Comer) wrote May 10, 2009
Pretty good article, you should remove the google js api reference, as that's beyond the scope of the article, and it could use a bit more fluff at the end of the article.
Andrew Photo
Andrew Johnson (ITNewb Guru) wrote May 9, 2009
Excellent work Frank! Thanks for teaching us this stuff and providing the examples =)

A+ from me!

Requirements

Nickname
  Remember me.
Login
Close