<?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>gEt DoWn ToNiGhT &#187; json</title>
	<atom:link href="http://www.getdowntonight.co.uk/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.getdowntonight.co.uk</link>
	<description>amusingly digressive progressive discussions</description>
	<lastBuildDate>Mon, 05 Dec 2011 22:41:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Sample Page for sending JSON data as string to web-service</title>
		<link>http://www.getdowntonight.co.uk/2011/12/sample-page-for-sending-json-data-as-string-to-web-service/</link>
		<comments>http://www.getdowntonight.co.uk/2011/12/sample-page-for-sending-json-data-as-string-to-web-service/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 16:56:04 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[sample]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=569</guid>
		<description><![CDATA[Just realised it has been a year since I posted anything on here! Time really flies when you&#8217;re busy! I remember when I was first starting off with jQuery, JSON and all that jazz that I struggled to find some simple examples of how to do things like send data to a web-service. Here is [...]]]></description>
			<content:encoded><![CDATA[<p>Just realised it has been a year since I posted anything on here! Time really flies when you&#8217;re busy!</p>
<p>I remember when I was first starting off with jQuery, JSON and all that jazz that I struggled to find some simple examples of how to do things like send data to a web-service. </p>
<p>Here is a quick example of how to send some JSON data (as a single post field) to a web service, using jQuery&#8217;s post method. </p>
<p>Keep in mind that if you are posting to another domain you may need to use something called jsonP &#8211; but if you are just learning, this is a good starting point!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;JSON Web Service Demo&lt;/title&gt;
&nbsp;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&nbsp;
        &lt;script src=&quot;http://code.jquery.com/jquery-1.7.1.min.js&quot;&gt;&lt;/script&gt;
&nbsp;
        <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">//this code will be added to the onclick event</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#save_user_button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//add the values we need for the API to an object</span>
                    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//build the object</span>
                    data.<span style="color: #660066;">guid</span>                   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#guid'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">defaultallocation</span>      <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#projecttype'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">email</span>                  <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#email'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">username</span>               <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#username'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    data.<span style="color: #660066;">fullname</span>               <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#fullname'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//turn the object in to a JSON string</span>
                    <span style="color: #003366; font-weight: bold;">var</span> myJson <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://testurl/service/user&quot;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">//call the post</span>
                    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>jsonUser <span style="color: #339933;">:</span> myJson<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>myResult<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myResult<span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div&gt;
            &lt;table&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;Project Type&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;projecttype&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;GUID&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;guid&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;User Name&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;username&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;Full Name&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;fullname&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
                &lt;tr&gt;
                    &lt;td&gt;Email&lt;/td&gt;
                    &lt;td&gt;&lt;input id=&quot;email&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
&nbsp;
            &lt;/table&gt;
&nbsp;
            &lt;input type=&quot;button&quot; id=&quot;save_user_button&quot; value=&quot;Save&quot;/&gt;
&nbsp;
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2011/12/sample-page-for-sending-json-data-as-string-to-web-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading JSON data &#8211; the EASY way!</title>
		<link>http://www.getdowntonight.co.uk/2009/11/loading-json-data-the-easy-way/</link>
		<comments>http://www.getdowntonight.co.uk/2009/11/loading-json-data-the-easy-way/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 12:22:04 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=398</guid>
		<description><![CDATA[A few weeks ago I wrote a blog post about using jQuery and JSON to automatically load data from a JSON file in to name-matched controls on an HTML page. It is only now that I realise quite how powerful this is! The great thing about it is that you do not have to edit [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-403 colorbox-398" title="json160" src="http://www.getdowntonight.co.uk/wp-content/json160.gif" alt="json160" width="96" height="96" />A few weeks ago I wrote a blog post about using jQuery and JSON to automatically load data from a JSON file in to name-matched controls on an HTML page.</p>
<p><strong>It is only now that I realise quite how powerful this is!</strong></p>
<p>The great thing about it is that you do not have to edit your data-load function if you add a new field to the database. Because the jQuery function loops through all the fields in the JSON data, it will automatically insert the values in to the correct fields, providing those fields are named the same as the html form controls.</p>
<p>Here is the code again for those who dont want to check back.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/yourJson.js, function(myJson){
	$.each(myJson.rows, function(i,item) {
		 for (prop in item) {
		 	 $('#' + prop).val(item[prop]);
		 }
	});
});</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2009/11/loading-json-data-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inserting JSON data in to name-matched controls</title>
		<link>http://www.getdowntonight.co.uk/2009/09/inserting-json-data-in-to-name-matched-controls/</link>
		<comments>http://www.getdowntonight.co.uk/2009/09/inserting-json-data-in-to-name-matched-controls/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 12:08:10 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=348</guid>
		<description><![CDATA[Thought this might be handy&#8230; This loop will insert the values from a JSON object in to text boxes. It loops around each key-pair and inserts the value if there is a text box with an ID that matches the key name. This is the JSON data: (yourJson.js) &#123;&#34;page&#34;:1,&#34;total&#34;:1,&#34;records&#34;:1,&#34;paging&#34;:20,&#34;rows&#34;:&#91; &#123;&#34;clipId&#34;:&#34;14792&#34;,&#34;barcode&#34;:&#34;02528&#34;,&#34;title&#34;:&#34;My Project Sample&#34;,&#34;format&#34;:&#34;DigiBeta&#34;,&#34;source&#34;:&#34;1242&#34;, &#34;runningTime&#34;:&#34;14 Mins&#34;,&#34;location&#34;:&#34;Library&#34;,&#34;timeCode&#34;:&#34;10:00:00&#34;,&#34;cUser&#34;:&#34;Sam&#34;, &#34;cDate&#34;:&#34;1 [...]]]></description>
			<content:encoded><![CDATA[<p>Thought this might be handy&#8230; This loop will insert the values from a JSON object in to text boxes.</p>
<p>It loops around each key-pair and inserts the value if there is a text box with an ID that matches the key name.</p>
<p>This is the JSON data: (yourJson.js)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;total&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;records&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;paging&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;rows&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;clipId&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;14792&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;barcode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;02528&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;My Project Sample&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;format&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;DigiBeta&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;source&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1242&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;runningTime&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;14 Mins&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;location&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Library&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;timeCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;10:00:00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;cUser&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Sam&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;cDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1 Jan 2007&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mUser&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Sam&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1 Jan 2008&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the form HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Clip Id<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'clipId'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Barcode<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'barcode'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Format<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'format'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Source<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'source'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Running Time<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'runningTime'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Location<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'location'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Time Code<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">'timeCode'</span><span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This is the jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/yourJson.js, function(myJson){
		$.each(myJson.rows, function(i,item) {
		    for (prop in item) {
				$('#' + prop).val(item[prop]);
		    }
		}); 
	});</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2009/09/inserting-json-data-in-to-name-matched-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating JSON data from MySQL in PHP</title>
		<link>http://www.getdowntonight.co.uk/2009/08/creating-json-data-from-mysql-in-php/</link>
		<comments>http://www.getdowntonight.co.uk/2009/08/creating-json-data-from-mysql-in-php/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 16:43:50 +0000</pubDate>
		<dc:creator>Sam</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.getdowntonight.co.uk/?p=331</guid>
		<description><![CDATA[Here is a quick post that demonstrates how to create some neat JSON data from MySQL using PHP. The script is really simple, and has been working well for me over the last few weeks. If anything is confusing, it might be worth checking out the previous post about creating XML data from PHP. It [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a quick post that demonstrates how to create some neat JSON data from MySQL using PHP. The script is really simple, and has been working well for me over the last few weeks. </p>
<p>If anything is confusing, it might be worth checking out the previous post about creating XML data from PHP. It explains things in more detail and will help if you are not familiar&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//set up the php headers so that the page doesnt cache etc</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last-Modified: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: no-cache, must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pragma: no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$totalPages</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">records</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$recordCount</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$countRows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userAccountId'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userName'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userName'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fullName'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fullName'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'accessCode'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'accessCode'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'addedBy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'addedBy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'addedDate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'addedDate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'modifiedBy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'modifiedBy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsonArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'modifiedDate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'modifiedDate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$i</span><span style="color: #339933;">++;</span> 
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jsonArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above code will create the following style JSON output.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;total&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;records&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;3&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;rows&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;userName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;fullName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Demo User&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;accessCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;demo@myexample.com&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;addedBy&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;addedDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2009-05-02 00:00:00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;modifiedBy&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;10&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;modifiedDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2009-06-04 00:00:00&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;userName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;sam&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;fullName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Sam Lasagne&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;accessCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;sam@myexample.com&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;addedBy&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;addedDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2009-05-02 00:00:00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;modifiedBy&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;modifiedDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2009-08-02 00:00:00&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;10&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;userName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;adrian&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;fullName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Adrian Spaghetti&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;accessCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;adrian@myexample.com&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;addedBy&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;addedDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2009-05-02 00:00:00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;modifiedBy&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;modifiedDate&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2009-09-01 00:00:00&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.getdowntonight.co.uk/2009/08/creating-json-data-from-mysql-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

