<?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>Themocracy WordPress Themes &#187; json</title>
	<atom:link href="http://themocracy.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://themocracy.com</link>
	<description>WordPress Theme Design</description>
	<lastBuildDate>Thu, 18 Aug 2011 07:34:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using JSON with WordPress</title>
		<link>http://themocracy.com/2010/02/json-wordpress/</link>
		<comments>http://themocracy.com/2010/02/json-wordpress/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 13:16:33 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://themocracy.com/?p=258</guid>
		<description><![CDATA[With the popularity of jQuery, AJAX, and client-side programming operations in general, there's more use made of JSON as a format for getting data backwards and forwards. 

For example, the Twitter API offers data as JSON  - and more manageably than accessing their RSS XML feeds for user timelines.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-right: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fthemocracy.com%2F2010%2F02%2Fjson-wordpress%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fthemocracy.com%2F2010%2F02%2Fjson-wordpress%2F" height="61" width="51" /></a></div><p><img src="http://themocracy.com/wp-content/uploads/2010/02/json.gif" alt="json with wordpress" title="json-wordpress" width="128" height="128" class="alignright size-full wp-image-259" />With the popularity of jQuery, AJAX, and client-side programming operations in general, there&#8217;s more use made of JSON as a format for getting data backwards and forwards. </p>
<p>For example, the Twitter API offers data as JSON  &#8211; and more manageably than accessing their RSS XML feeds for user timelines etc.</p>
<pre>http://twitter.com/status/user_timeline/USERNAME.json?count=10 </pre>
<p>The issue is how to make use of JSON-encoded information in the WordPress environment &#8211; much of what is available is only recently available, so some care has to be taken about how it&#8217;s used.</p>
<h3>1. Native PHP functions</h3>
<p><strong>json_encode()</strong> and <strong>json_decode()</strong> make life very simple &#8211; but these functions are only available in PHP >= 5.2.0 and WordPress is still designed to run on significantly older versions of PHP than this. Alternatives would have to be supplied if you were thinking about releasing a plugin. There are custom-written functions to reproduce these in PHP4, see for example user contributions to <a href="http://php.net/manual/en/function.json-encode.php">json_encode</a>. These are often not 100% perfect, but one may well do all that you require&#8230;</p>
<h3>2. JSON2</h3>
<p>If you&#8217;re happy to do the work in jQuery/javaScript, WordPress 2.9 comes with JSON2 bundled, registered and ready to go&#8230;</p>
<pre class="brush: php">
add_action(&#039;wp_print_scripts&#039;,&#039;load_json_parser&#039;);

function load_json_parser(){

	if (is_admin()) return; // probably don&#039;t want this on admin pages

	wp_enqueue_script(&#039;json2&#039;);

}
</pre>
<p>No need any more to play around with eval in the javaScript, but again, this is a WordPress >= 2.9 thing&#8230;. </p>
<h3>3. Borrowing the Text Editor</h3>
<p>Lastly, there is one more, slightly unofficial, dodge that can be used. WordPress makes use of TinyMCE as its rich text editor and this comes with the its own JSON parser. Results may not be completely predictable. </p>
<pre class="brush: php">
require_once(ABSPATH.&quot;/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php&quot;);

$jsObj = new Moxiecode_JSON();

// encodes the supplied array to JSON format
$json = $jsObj-&gt;encode(array(&quot;key1&quot;=&gt;&quot;value1&quot;,&quot;key2&quot;=&gt;&quot;value2&quot;));

//decodes supplied JSON to a PHP array
$json_array = $jsObj-&gt;decode($json);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://themocracy.com/2010/02/json-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

