hubbs ha scritto:
I am trying to understand JSON, and could use a little help.


How would I use jQuery to write the values onto a page?
user = {

    "firstName": "John",
    "lastName": "Smith",
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        "212 555-1234",
        "646 555-4567"
    ]
}


alert(user.firstName);
alert(user.address.city);
  And how do
you go the other way, taking values and serializing them into JSON to
be sent to the server?
Umh... I'm not sure I've understood the question.

You don't have to send json data to the server, but the server sends json data to the client.

For example, if you do an ajax request to a page.php that do a query to the db, and return the data, you'll user the function json_encode (php5, or PEAR on php4) to write the response for javascript.

<?php
$record['firstName'] = "John";
$record['address']['city'] = "New York";
echo json_encode($record);
?>

Bye :-)

--
gianiaz.net - web solutions
p.le bertacchi 66, 23100 sondrio (so) - italy
+39 347 7196482

Reply via email to