Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] My first JSON object



On Sat, Jun 19, 2010 at 12:02:32PM +0900, Dave M G wrote:
> TLUG,
...
> Okay, so, I think I can create a JSON in Javascript in the following manner.
> 
> var myJSON = {"pet" = "dog",
>          "car" = "hybrid"};

this is not JSON.

> Cool. Nice and easy. Now I've got "myJSON" ready to go.

well, there is no such thing like a JSON object. You should think of
JSON as some sort of serialization format or (per the acronym) a
notation to describe objects.
So you have an hastable, array or object of some sort and serialize it
in JSON format (you could have chosen YAML or XML or...).

> JSONrequest = JSONRequest.post("http://myserver.com/jsonhandler.php";, 
> myJSON);

have a look at the documentation: http://www.json.org/JSONRequest.html
JSONRequest will serialize the object in JSON format and post the
resulting text to the URL you provided.

> On the server, jsonhandler.pho decodes the JSON object into a PHP array.
...do things in your PHP page and produce...
                      ↓
> $JSONarrayRevised = json_encode($dataFromMySQL);
...
> I want to output that JSON data in such a way so that the client will 
> pick it up.
> 
> If I just output it, will the client be able to and parse that data? If 
> so, how, exactly?

You can output anything you want, what you get back in your javascript
piece is just a string containing the data produced by the PHP page. It
can be some html snippet, xml, yaml, cvs, anything... it's up to the
receiver of that text to do anything meaningful/useful with it.

> I know that a number gets stored in that JSONrequest variable I created 
> at the start. But I don't think that number is useful to me in this 
> case... or is it?

It actually is. You can either use that or some anonymous function to
process the response from the server. Look at the documentation again,
there are examples for how to handle the returned text.

> Can anyone help me get that last step of sending the PHP created JSON 
> object to the client?

<?= $JSONarrayRevised ?> 
(if my php recollection is correct) will do.

As said JSONRequest does nothing on its own with the response from the
server. You will have to take the text you printed in the PHP page and
turn it into a real object on the javascript side:
http://www.json.org/js.html
This and the previoud link have enough examples to give you a clear idea
on how to obtail what you're after.

One suggestion: use firebug or anything (i.e.: a dumb tunnel) that can
show you the actual request and response to/from the server.
I would also recommend using some existing library to do all this, it
will abstract away some of the complexity and it usually does a good job
in terms of browser compatibility.
Prototype Javascript is one of them, see the documentation for
Ajax.Request and in particular the section about "Automatic JavaScript
response evaluation", you may have to tweak the content type header of
the response from the php page... Maybe I'm going too far now :)
-- 
mattia
:wq!


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links