firstName lastName zipCode
John Doe 20007
Mary Doe 20016
People (with a date attribute)
|_ Person
|_firstName
|_lastName
|_zipCode
<?xml version="1.0" encoding="utf-8"?>
<testData>
<people listDate="2015-11-08">
<person>
<firstName>John</firstName>
<lastName>Doe</lastName>
<zipCode>20007</zipCode>
</person>
<person>
<firstName>Mary</firstName>
<lastName>Doe</lastName>
<zipCode>20016</zipCode>
</person>
</people>
</testData>
<?php
$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<testData>
<people listDate="2015-11-08">
<person>
<firstName>John</firstName>
<lastName>Doe</lastName>
<zipCode>20007</zipCode>
</person>
<person>
<firstName>Mary</firstName>
<lastName>Doe</lastName>
<zipCode>20016</zipCode>
</person>
</people>
</testData>
EOD;
// CREATE THE OBJECT
$obj = SimpleXML_Load_String($xml);
// VISUALIZE THE OBJECT
var_dump($obj);
// GET THE DATE ATTRIBUTE FROM THE PEOPLE NODE OF THE OBJECT
echo PHP_EOL . $obj->people['listDate'];
// EXTRACT DETAILS FROM THE OBJECT
foreach ($obj->people->person as $p)
{
if ($p->lastName == 'Doe')
{
echo PHP_EOL . "$p->firstName $p->lastName $p->zipCode";
}
}
object(SimpleXMLElement)#1 (1) {
["people"]=>
object(SimpleXMLElement)#2 (2) {
["@attributes"]=>
array(1) {
["listDate"]=>
string(10) "2015-11-08"
}
["person"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#3 (3) {
["firstName"]=>
string(4) "John"
["lastName"]=>
string(3) "Doe"
["zipCode"]=>
string(5) "20007"
}
[1]=>
object(SimpleXMLElement)#4 (3) {
["firstName"]=>
string(4) "Mary"
["lastName"]=>
string(3) "Doe"
["zipCode"]=>
string(5) "20016"
}
}
}
}
{
"people": {
"@attributes": {
"listDate": "2015-11-08"
},
"person": [
{
"firstName": "John",
"lastName": "Doe",
"zipCode": "20007"
},
{
"firstName": "Mary",
"lastName": "Doe",
"zipCode": "20016"
}
]
}
}
<?php
$jso = <<<EOD
{
"people": {
"@attributes": {
"listDate": "2015-11-08"
},
"person": [
{
"firstName": "John",
"lastName": "Doe",
"zipCode": "20007"
},
{
"firstName": "Mary",
"lastName": "Doe",
"zipCode": "20016"
}
]
}
}
EOD;
// CREATE THE OBJECT
$obj = json_decode($jso);
// VISUALIZE THE OBJECT
var_dump($obj);
// GET THE DATE ATTRIBUTE FROM THE PEOPLE NODE OF THE OBJECT
echo PHP_EOL . $obj->people->{'@attributes'}->listDate;
// EXTRACT DETAILS FROM THE OBJECT
foreach ($obj->people->person as $p)
{
if ($p->lastName == 'Doe')
{
echo PHP_EOL . "$p->firstName $p->lastName $p->zipCode";
}
}
object(stdClass)#1 (1) {
["people"]=>
object(stdClass)#2 (2) {
["@attributes"]=>
object(stdClass)#3 (1) {
["listDate"]=>
string(10) "2015-11-08"
}
["person"]=>
array(2) {
[0]=>
object(stdClass)#4 (3) {
["firstName"]=>
string(4) "John"
["lastName"]=>
string(3) "Doe"
["zipCode"]=>
string(5) "20007"
}
[1]=>
object(stdClass)#5 (3) {
["firstName"]=>
string(4) "Mary"
["lastName"]=>
string(3) "Doe"
["zipCode"]=>
string(5) "20016"
}
}
}
}
<?php
$jso = <<<EOD
{
"people": {
"@attributes": {
"listDate": "2015-11-08"
},
"person": [
{
"firstName": "John",
"lastName": "Doe",
"zipCode": "20007"
},
{
"firstName": "Mary",
"lastName": "Doe",
"zipCode": "20016"
}
]
}
}
EOD;
// CREATE THE ARRAY INSTEAD OF THE OBJECT
$arr = json_decode($jso, TRUE);
// VISUALIZE THE OBJECT
var_dump($arr);
// GET THE DATE ATTRIBUTE FROM THE PEOPLE NODE OF THE OBJECT
echo PHP_EOL . $arr['people']['@attributes']['listDate'];
// EXTRACT DETAILS FROM THE OBJECT
foreach ($arr['people']['person'] as $p)
{
if ($p['lastName'] == 'Doe')
{
echo PHP_EOL . $p['firstName'] . ' ' . $p['lastName'] . ' ' . $p['zipCode'];
}
}
<?php
$jso = <<<EOD
{
"people": {
"@attributes": {
"listDate": "2015-11-08"
},
"person": [
{
"firstName": "John",
"lastName": "Doe",
"zipCode": "20007"
},
{
"firstName": "Mary",
"lastName": "Doe",
"zipCode": "20016"
}
]
}
}
EOD;
// CREATE THE OBJECT AND ARRAY
$obj = json_decode($jso);
$arr = json_decode($jso, TRUE);
// RETURN THE PHP DATA TO JSON-ENCODED FORMAT
$json_obj = json_encode($obj);
$json_arr = json_encode($arr);
// COMPARE THE NEW JSON STRINGS
if ($json_obj === $json_arr) echo 'Success!'; // Success!
<script>
var jso =
{
"people": {
"@attributes": {
"listDate": "2015-11-08"
},
"person": [
{
"firstName": "John",
"lastName": "Doe",
"zipCode": "20007"
},
{
"firstName": "Mary",
"lastName": "Doe",
"zipCode": "20016"
}
]
}
}
;
alert( 'Date attribute: ' + jso.people['@attributes'].listDate );
alert( 'Mary is in the "person" array at position one: ' + jso.people.person[1].lastName + ' ' + jso.people.person[1].zipCode );
</script>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<title>AJAX/JSON Example Using jQuery</title>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
$.get("json_object_server.php", function(jsonString){
var jsonObject = JSON.parse(jsonString);
$("#output p#name").html(jsonObject.name + ' ' + jsonObject.surName);
$("#output p#salary").html(jsonObject.salary);
});
});
</script>
</head>
<body>
<div id="output">
<p id="name" />
<p id="salary" />
</div>
</body>
</html>
while ($row = $res->fetch_object()) echo json_encode($row);
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Voting Yes plus recommended as Featured Article.
Commented:
Great article (as always) :). I just wanted to address the "Using JSON Strings in JavaScript" section of your article.
I'd like to see the distinction between JavaScript and jQuery/AngularJS to show that jQuery & AngularJS are frameworks of the JavaScript language. In otherwords, they are just JavaScript. They do not implement their own version of the ECMA-262 standards. Node.js on the other hand DOES have its own version but being only a server side language, that's a different story.
On another note, JSON in the browser still needs to be decoded from a string to be a useful JavaScript native object. Any data that PHP returns in JSON format will need to be decoded using the JSON.parse() native JavaScript function. jQuery will do this automatically when the getJSON() or ajax() with the dataType set to "json" but vanilla JavaScript will not. For your reference: http://www.w3schools.com/js/js_json.asp
Following on from this, the example you go on to give isn't a string, but a JS object. I'd like to see an example of using the native functions for converting JSON strings to objects as indicated here: http://www.w3schools.com/js/js_json.asp
Author
Commented: