<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Made Simple &#187; json</title>
	<atom:link href="http://www.phpmadesimple.info/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmadesimple.info</link>
	<description>To support simplicity, possiibility and cost effective of PHP</description>
	<lastBuildDate>Tue, 03 Aug 2010 14:13:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More about JSON</title>
		<link>http://www.phpmadesimple.info/2008/04/27/more-about-json/</link>
		<comments>http://www.phpmadesimple.info/2008/04/27/more-about-json/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 09:06:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[oop with json]]></category>

		<guid isPermaLink="false">http://artemis.com.vn/blogvui/index.php/2008/04/27/more-about-json/</guid>
		<description><![CDATA[After the first post, JSON Quick start, you know what JSON is and how useful it&#8217;s in AJAX applications. This is more about JSON with more examples and advanced usages.
Using JSON
The JavaScript Object Notation (JSON) is a core feature of the language. It provides a concise mechanism for creating arrays and object graphs. In order [...]]]></description>
			<content:encoded><![CDATA[<p>After the first post, JSON Quick start, you know what JSON is and how useful it&#8217;s in AJAX applications. This is more about JSON with more examples and advanced usages.<span id="more-31"></span></p>
<h1>Using JSON</h1>
<p>The JavaScript Object Notation (JSON) is a core feature of the language. It provides a concise mechanism for creating arrays and object graphs. In order to understand JSON, we need to know how JavaScript arrays work, so let&#8217;s cover the basics of them first.</p>
<p>JavaScript has a built-in Array class that can be instantiated using the new keyword:</p>
<pre class="brush: javascript">

myLibrary.books=new Array();
</pre>
<p>Arrays can have values assigned to them by number, much like a conventional C or Java array:</p>
<pre class="brush: javascript">

myLibrary.books[4]=somePredefinedBook;
</pre>
<p>Or they can be associated with a key value, like a Java Map or Python Dictionary, or, indeed, any JavaScript Object:</p>
<pre class="brush: javascript">
myLibrary.books[&quot;BestSeller&quot;]=somePredefinedBook;
</pre>
<p>This syntax is good for fine-tuning, but building a large array or object in the first place can be tedious. The shorthand for creating a numerically indexed array is to use square braces, with the entries being written as a comma-separated list of values, thus:</p>
<pre class="brush: javascript">
myLibrary.books=[predefinedBook1,predefinedBook2,predefinedBook3];
</pre>
<p>And to build a JavaScript Object, we use curly braces, with each value written as a key:value pair:</p>
<pre class="brush: javascript">
myLibrary.books={

bestSeller : predefinedBook1,

cookbook : predefinedBook2,

spaceFiller : predefinedBook3

};
</pre>
<p>In both notations, extra white space is ignored, allowing us to pretty-print for clarity. Keys can also have spaces in them, and can be quoted in the JSON notation, for example:</p>
<p>&#8220;Best Seller&#8221; : predefinedBook1,</p>
<p>We can nest JSON notations to create one-line definitions of complex object hierarchies (albeit rather a long line):</p>
<pre class="brush: javascript">
var myLibrary={

location : &quot;my house&quot;,

keywords : [ &quot;root vegetables&quot;, &quot;turnip&quot;, &quot;tedium&quot; ],

books: [

{

title : &quot;Turnip Cultivation through the Ages&quot;,

authors : [

{ name: &quot;Jim Brown&quot;, age: 9 },

{ name: &quot;Dick Turnip&quot;, age: 312 }

],

publicationDate : &quot;long ago&quot;

},

{

title : &quot;Turnip Cultivation through the Ages, vol. 2&quot;,

authors : [

{ name: &quot;Jim Brown&quot;, age: 35 }

],

publicationDate : new Date(1605,11,05)

}

]

};
</pre>
<p>Sharp-eyed readers will have noted that we populated the publication date for the second book using a JavaScript Date object. In assigning the value we can use any JavaScript code, in fact, even a function that we defined ourselves:</p>
<pre class="brush: javascript">
function gunpowderPlot(){

return new Date(1605,11,05);

}

var volNum=2;

var turnipVol2={

title : &quot;Turnip Cultivation through the Ages, vol. &quot;+volNum,

authors : [

{ name: &quot;Jim Brown&quot;, age: 35 }

],

publicationDate : gunpowderPlot()

}
</pre>
<p>We can also define member functions for our JSON-invoked objects, which can be invoked later by the object:</p>
<pre class="brush: javascript">
var turnipVol2={

title : &quot;Turnip Cultivation through the Ages, vol. &quot;+volNum,

authors : [

{ name: &quot;Jim Brown&quot;, age: 35 }

],

publicationDate : gunpowderPlot()

},

summarize:function(len){

if (!len){ len=7; }

var summary=this.title+&quot; by &quot; + this.authors[0].name +&quot; and his cronies is very boring. Z&quot;;

for (var i=0;i&lt; len;i++){

summary+=&quot;z&quot;;

}

alert(summary);

}

};

...

turnipVol2.summarize(6);
</pre>
<p>The summarize() function has all the features of a standard JavaScript function, such as parameters and a context object identified by the keyword this. Indeed, once the object is created, it is just another JavaScript object, and we can mix and match the JavaScript and JSON notations as we please. We can use JavaScript to fine-tune an object declared in JSON:</p>
<pre class="brush: javascript">
var numbers={ one:1, two:2, three:3 };

numbers.five=5;
</pre>
<p>We initially define an object using JSON syntax and then add to it using plain JavaScript. Equally, we can extend our JavaScript-created objects using JSON:</p>
<pre class="brush: javascript">
var cookbook=new Object();

cookbook.pageCount=321;

cookbook.author={

firstName: &quot;Harry&quot;,

secondName: &quot;Christmas&quot;,

birthdate: new Date(1900,2,29),

interests: [&quot;cheese&quot;,&quot;whistling&quot;,&quot;history of lighthouse keeping&quot;]

};
</pre>
<p>With the built-in JavaScript Object and Array classes and the JSON notation, we can build object hierarchies as complicated as we like, and we could get by with nothing else. JavaScript also offers a means for creating objects that provides a comforting resemblance to class definitions for OO programmers, so let&#8217;s look at this next and see what it can offer us.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmadesimple.info/2008/04/27/more-about-json/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSON quick start</title>
		<link>http://www.phpmadesimple.info/2008/01/19/json-quick-start/</link>
		<comments>http://www.phpmadesimple.info/2008/01/19/json-quick-start/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 05:13:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax with json]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[level 1]]></category>
		<category><![CDATA[object notation]]></category>

		<guid isPermaLink="false">http://artemis.com.vn/blogvui/?p=23</guid>
		<description><![CDATA[What does it stand for?
JavaScript Object Notation.
According to Douglas Corckford’s website, JSON is a lightweight data-interchange format. It is easy for humans to read and write… JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, [...]]]></description>
			<content:encoded><![CDATA[<h3>What does it stand for?</h3>
<p>JavaScript Object Notation.</p>
<p>According to Douglas Corckford’s website, JSON is a lightweight data-interchange format. It is easy for humans to read and write… JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.&#8221;</p>
<p>Object Notation can be expressed in the following format:</p>
<pre class="brush: javascript">
var obj = {
// string
a : &#039;sampleValue&#039;,
// array
b : [obj1,obj2,&#039;three&#039;,4,obj5],
// function
c : function() {
var bang = this.a;
}
}
</pre>
<h3>JSON is built on two structures:</h3>
<ul>
<li>A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.</li>
<li>An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.</li>
</ul>
<h3>Reasons for JSON</h3>
<ul>
<li>JSON is easy. No really. It’s so easy, it’ll make you sick.</li>
<li>If you’re familiar with writing classes in PHP, then you’ll most definitely be comfortable with writing JavaScript in Object Notation</li>
<li>JSON is nothing more than name : value pairs assigned within an object.</li>
<li>JSON is easy to understand because if written well, it’s a self-documenting structure.</li>
<li>JSON is fast!</li>
<li>JSON organizes the ugly mess of procedural programming. Imagine having more than one init function.</li>
<li>You can impress your friends with JSON because it’s pretty looking</li>
<li>Your co-workers will love you for writing in JSON because it will most likely not conflict with their scripts that are being called within the same web documents.</li>
</ul>
<h3>JSON is like XML because:</h3>
<ol>
<li>They are both &#8217;self-describing&#8217; meaning that values are named, and thus &#8216;human readable&#8217;</li>
<li>Both are hierarchical. (i.e. You can have values within values.)</li>
<li>Both can be parsed and used by lots of programming languages</li>
<li>Both can be passed around using AJAX (i.e. httpWebRequest)</li>
</ol>
<h3>JSON is UNlike XML because:</h3>
<ol>
<li>XML uses angle brackets, with a tag name at the start and end of an element: JSON uses squiggly brackets with the name only at the beginning of the element.</li>
<li>JSON is less verbose so it&#8217;s definitely quicker for humans to write, and probably quicker for us to read.</li>
<li>JSON can be parsed trivially using the eval() procedure in JavaScript</li>
<li>JSON includes arrays {where each element doesn&#8217;t have a name of its own}</li>
<li>In XML you can use any name you want for an element, in JSON you can&#8217;t use reserved words from javascript</li>
</ol>
<h3>But Why? What&#8217;s good about it?</h3>
<p>When you&#8217;re writing ajax stuff, if you use JSON, then you avoid hand-writing xml. This is quicker. Also, with ajax services support JSON output, you can bypass difficulties of cross-domain ajax calls.</p>
<p>Again, when you&#8217;re writing ajax stuff, which looks easier? the XML approach or the JSON approach:<br />
The XML approach:</p>
<ol>
<li>bring back an XML document</li>
<li>loop through it, extracting values from it</li>
<li>do something with those values, etc,</li>
</ol>
<p>versus the JSON approach:</p>
<ol>
<li>bring back a JSON string.</li>
<li>&#8216;eval&#8217; the JSON</li>
</ol>
<h3>Example of using JSON in AJAX to transmit data</h3>
<p>The example is about to submit a form to a PHP page using AJAX. To keep the example simple, no data is sent (through POST) to the PHP page. Rather, data is just retrieved from the PHP page. The JSMX javascript library is very lightweight (code-wise) and sends and retrieves our data for us. The PHP page (that we make our requests to) simply prints out the data it wants to send back in a ready-to-use javascript object format.</p>
<p>&lt;img src=&#8221;http://simpletutorials.com/tutorials/javascript/simple_ajax/ajax_diagram.jpg&#8221; alt=&#8221;" /&gt;</p>
<pre class="brush: php">
&lt; ?php

require_once &quot;json/JSON.php&quot;;
$json = new Services_JSON();

//convert php object to json

$value = array(&#039;first&#039; =&gt; &#039;Steven&#039;, &#039;last&#039; =&gt; &#039;Spielberg&#039;, &#039;address&#039; =&gt; &#039;1234 Unlisted Drive&#039;);

$output = $json-&gt;encode($value);

print($output);

?&gt;
</pre>
<p>The JSON-PHP module can convert a PHP array of key value pairs to this JSON object.</p>
<pre class="brush: javascript">
{&quot;first&quot;:&quot;Steven&quot;,&quot;last&quot;:&quot;Spielberg&quot;,&quot;address&quot;:&quot;1234 Unlisted Drive&quot;}
</pre>
<p>HTML page</p>
<pre class="brush: html">
&lt;html&gt;
&lt;head&gt;

&lt;script src=&quot;ajax.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;

/**Ajax Request (Submits the form below through AJAX
*               and then calls the ajax_response function)
*/
function ajax_request() {
var submitTo = &#039;ajax_request.php&#039;;
//location.href = submitTo; //uncomment if you need for debugging

http(&#039;POST&#039;, submitTo, ajax_response, document.form1);
}

/**Ajax Response (Called when ajax data has been retrieved)
*
* @param   object  data   Javascript (JSON) data object received
*                         through ajax call
*/
function ajax_response(data) {
for(var key in data) {
document.form1[key].value = data[key];
}
}

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;input type=&quot;button&quot; onclick=&quot;ajax_request()&quot; value=&quot;Do AJAX&quot;/&gt;&lt;br /&gt;&lt;br /&gt;

&lt;form name=&quot;form1&quot;&gt;
First &lt;input type=&quot;text&quot; name=&quot;first&quot;/&gt;&lt;br /&gt;
Last  &lt;input type=&quot;text&quot; name=&quot;last&quot;/&gt;&lt;br /&gt;
Address &lt;input type=&quot;text&quot; name=&quot;address&quot;/&gt;&lt;br /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This tutorial demonstrates how to perform AJAX functionality simply and effectively, using the <a href="http://www.lalabird.com/" target="_blank">AJAX JSMX library</a>, coupled with the <a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198" target="_blank">JSON-PHP  library</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmadesimple.info/2008/01/19/json-quick-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
