* Fun fact:
PHP has a number of XML-related packages
* one of these is SimpleXML
(http://php.net/simplexml)
* let's say you have a file (on the application
tier) named play.xml
$myXml = simplexml_load_file("play.xml");
* $myXML is now a PHP object representing
the XML in file play.xml
* I can grab contents of XML element elm
with the expression:
$myXml->elem
* JSON!
* "discovered" by Douglas Crockford
* JSON stands for "JavaScript Object Notation"
* it is not EXACTLY JavaScript's object syntax --
but it is BASED on JavaScript's object syntax
* it is a plain-text data format
* ASIDE: JavaScript Object SYNTAX:
* in C++, or Java,
you create a class,
and an object is what you get when you
create an instance of that class;
* In JavaScript -- no classes?!
"you just declare objects one at a time
and can then use them"
...you can write object literals!
{
fieldname: value,
fieldname: value,
...
fieldname: value
}
var aPerson =
{
name: "Phillip J. Fry",
age: 23,
weight: 172.5,
friends: ["Farnsworth", "Hermes",
"Zoidberg"],
getBeloved: function()
{
return this.name +
" loves Leela";
}
};
aPerson.age == 23
aPerson["weight"] == aPerson.weight
aPerson["weight"] == 172.5
aPerson.getBeloved() == "Phillip J. Fry loves Leela"
[wondering:
would this work:
aPerson["getBeloved"]()
?? NOT SURE!!!!!
]
* the above is talking about JavaScript object SYNTAX;
NOW let's go back to JSON
* in JSON...
JSON represents data using a syntax/notation that is
SIMILAR to the syntax for JavaScript object literals
* what are some DIFFERENCES?
* ALL JSON object property names MUST be enclosed
in quotes
* JSON objects may NOT have functions as
properties
* SOME characters are forbidden as JSON data for
compatibility reasons
* BUT, JavaScript CAN take *text* in this notation
and easily convert it to JavaScript executable code;
var myData = JSON.parse(myJSONData);
* myJSONData containing a JSON string
is converted above into an equivalent
JavaScript object, above stored into
the variable myData
...and now:
myData.messages[0].time ...should be "1:57"
* and if you want to convert a JavaScript object
into a string of JSON data,
var myJSONString = JSON.stringify(myJavaScriptObject);
* example of using JSON outside of JavaScript:
PHP has a JSON extension
(http://php.net/manual/en/book.json.php)
* if you have a JSON string:
$myJSON = '{"sound": "moo", "volume": 13}';
* you can get the value in that JSON decoded into
an appropriate PHP object using json_decode:
$phpVersion = json_decode($myJSON);
...so now I can handle this PHP object like
a PHP object:
($phpVersion->{'sound'}) === "moo"
($phpVersion->{'volume'}) === 13