<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Format JSON with PHP</title>
	<atom:link href="http://recurser.com/articles/2008/03/11/format-json-with-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://recurser.com/articles/2008/03/11/format-json-with-php/</link>
	<description></description>
	<lastBuildDate>Wed, 10 Mar 2010 16:39:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ways to Format and View JSON Online - WittySparks</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-1457</link>
		<dc:creator>Ways to Format and View JSON Online - WittySparks</dc:creator>
		<pubDate>Sun, 31 Jan 2010 17:11:04 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-1457</guid>
		<description>[...]  Format JSON with PHP [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;]  Format <span class="caps">JSON</span> with <span class="caps">PHP</span> [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: drapeko</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-1371</link>
		<dc:creator>drapeko</dc:creator>
		<pubDate>Mon, 07 Dec 2009 20:44:45 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-1371</guid>
		<description>Thanks for the code, quite useful when you are lazy to do yourself :)</description>
		<content:encoded><![CDATA[<p>Thanks for the code, quite useful when you are lazy to do yourself :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andreas</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-1142</link>
		<dc:creator>Andreas</dc:creator>
		<pubDate>Tue, 27 Oct 2009 15:43:51 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-1142</guid>
		<description>Hi Davev,
your Code from 2009/10/03 produces only errors if I copy/paste it. Could you Post it without all this typing errors? It would be very kind of you.</description>
		<content:encoded><![CDATA[<p>Hi Davev,<br />
your Code from 2009/10/03 produces only errors if I copy/paste it. Could you Post it without all this typing errors? It would be very kind of you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davey Shafik</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-1131</link>
		<dc:creator>Davey Shafik</dc:creator>
		<pubDate>Sun, 04 Oct 2009 04:29:50 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-1131</guid>
		<description>This modified version will ignore special characters in quoted strings.

	public static function indent($json) {

		$result    = &#039;&#039;;
		$pos       = 0;
		$strLen    = strlen($json);
		$indentStr = &#039;  &#039;;
		$newLine   = &quot;\n&quot;;

		for($i = 0; $i &lt;= $strLen; $i  ) {

			// Grab the next character in the string
			$char = substr($json, $i, 1);

			// Skip quoted keys and values
			if ($char == &#039;&quot;&#039; &#124;&#124; $char == &quot;&#039;&quot;) {
				echo &quot;Found $char at position $i&quot;;
				$lookfor = $char;
				for ($i = $i 1; $i &lt;= $strLen; $i  ) {
					$result .= $char;
					$char = substr($json, $i, 1);
					if ($char == $lookfor) {
						echo &quot; and closing $char at $i&quot;;
						break;
					}
				}
			}

			// If this character is the end of an element,
			// output a new line and indent the next line
			if($char == &#039;}&#039; &#124;&#124; $char == &#039;]&#039;) {
				$result .= $newLine;
				$pos --;
				for ($j=0; $j&lt;$pos; $j  ) {
					$result .= $indentStr;
				}
			}

			// Add the character to the result string
			$result .= $char;

			// If the last character was the beginning of an element,
			// output a new line and indent the next line
			if ($char == &#039;,&#039; &#124;&#124; $char == &#039;{&#039; &#124;&#124; $char == &#039;[&#039;) {
				$result .= $newLine;
				if ($char == &#039;{&#039; &#124;&#124; $char == &#039;[&#039;) {
					$pos   ;
				}
				for ($j = 0; $j &lt; $pos; $j  ) {
					$result .= $indentStr;
				}
			}
		}

		return $result;
	}</description>
		<content:encoded><![CDATA[<p>This modified version will ignore special characters in quoted strings.</p>
<p>	public static function indent($json) {</p>
<p>		$result    = &#8216;&#8217;;<br />
		$pos       = 0;<br />
		$strLen    = strlen($json);<br />
		$indentStr = &#8216;  &#8216;;<br />
		$newLine   = &#8220;\n&#8221;;</p>
<p>		for($i = 0; $i &lt;= $strLen; $i  ) {</p>
<p>			// Grab the next character in the string<br />
			$char = substr($json, $i, 1);</p>
<p>			// Skip quoted keys and values<br />
			if ($char  &#039;&quot;&#039; || $char  &quot;&#039;&quot;) {<br />
				echo &quot;Found $char at position $i&quot;;<br />
				$lookfor = $char;<br />
				for ($i = $i 1; $i &lt;= $strLen; $i  ) {<br />
					$result .= $char;<br />
					$char = substr($json, $i, 1);<br />
					if ($char == $lookfor) {<br />
						echo &quot; and closing $char at $i&quot;;<br />
						break;<br />
					}<br />
				}<br />
			}</p>
<p>			// If this character is the end of an element,<br />
			// output a new line and indent the next line<br />
			if($char  &#039;}&#039; || $char  &#039;]&#039;) {<br />
				$result .= $newLine;<br />
				$pos &#8212;;<br />
				for ($j=0; $j&lt;$pos; $j  ) {<br />
					$result .= $indentStr;<br />
				}<br />
			}</p>
<p>			// Add the character to the result string<br />
			$result .= $char;</p>
<p>			// If the last character was the beginning of an element,<br />
			// output a new line and indent the next line<br />
			if ($char  &#039;,&#039; || $char  &#039;{&#039; || $char  &#039;[&#039;) {<br />
				$result .= $newLine;<br />
				if ($char  &#039;{&#039; || $char == &#039;[&#039;) {<br />
					$pos   ;<br />
				}<br />
				for ($j = 0; $j &lt; $pos; $j  ) {<br />
					$result .= $indentStr;<br />
				}<br />
			}<br />
		}</p>
<p>		return $result;<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yukulélé</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-1066</link>
		<dc:creator>Yukulélé</dc:creator>
		<pubDate>Tue, 21 Jul 2009 09:40:19 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-1066</guid>
		<description>if their is &quot;{[}]&quot; chars in a string eements, theiy ar indented 
exemple :
indent(&#039;{&quot;a&quot;:1,&quot;b&quot;:&quot;[1{}23]&quot;}&#039;);

{
  &quot;a&quot;:1,
  &quot;b&quot;:&quot;[
    1{
      
    }23
  ]&quot;
}</description>
		<content:encoded><![CDATA[<p>if their is &#8220;{[}]&#8221; chars in a string eements, theiy ar indented <br />
exemple :<br />
indent(&#8216;<a href="hu1,&#34;b&#34;:&#34;[1">a</a>{23]&#8221;}&#8217;);</p>
<p>{<br />
  &#8220;a&#8221;:1,<br />
  &#8220;b&#8221;:&#8221;[<br />
    1{<br />
      <br />
    }23<br />
  ]&#8221;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mithun Dhali</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-990</link>
		<dc:creator>Mithun Dhali</dc:creator>
		<pubDate>Sat, 16 May 2009 05:49:02 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-990</guid>
		<description>There is an major issue with the code. It does not take into consideration string escaping.

If a string value (inside quotes) has &#039;,&#039; (COMMA) in it, it simply puts a newline after it. When doing an eval, this will give an unterminated error.

Example input: 
{&quot;code&quot;:&quot;S_UPDATE_OK&quot;,&quot;success&quot;:true,&quot;params&quot;:{&quot;part&quot;:&quot;school&quot;,&quot;entity&quot;:&quot;resume&quot;,&quot;html&quot;:&quot;&quot;}}

Example output: (Note that in the value html an unwanted newline is added after COMMA.)
{
  &quot;code&quot;:&quot;S_UPDATE_OK&quot;,
  &quot;success&quot;:true,
  &quot;params&quot;:{
    &quot;part&quot;:&quot;school&quot;,
    &quot;entity&quot;:&quot;resume&quot;,
    &quot;html&quot;:&quot;&quot;
  }
}

The code may work for simple strings, however it will give unexpected errors for complex data in string values.</description>
		<content:encoded><![CDATA[<p>There is an major issue with the code. It does not take into consideration string escaping.</p>
<p>If a string value (inside quotes) has &#8216;,&#8217; (<span class="caps">COMMA</span>) in it, it simply puts a newline after it. When doing an eval, this will give an unterminated error.</p>
<p>Example input: <br />
<a href="&#34;S_UPDATE_OK&#34;,&#34;success&#34;:true,&#34;params&#34;:{&#34;part&#34;:&#34;school&#34;,&#34;entity&#34;:&#34;resume&#34;,&#34;html">code</a>&#8221;:&#8221;&#8220;}</p>
<p>Example output: (Note that in the value html an unwanted newline is added after <span class="caps">COMMA</span>.)<br />
{<br />
  <a href="&#34;S_UPDATE_OK">code</a>&#8221;,<br />
  <a href="hutrue">success</a>,<br />
  <a href="{">params</a><br />
    <a href="&#34;school">part</a>&#8221;,<br />
    <a href="&#34;resume">entity</a>&#8221;,<br />
    <a href="&#34;">html</a>&#8221;<br />
  }<br />
}</p>
<p>The code may work for simple strings, however it will give unexpected errors for complex data in string values.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Higson</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-982</link>
		<dc:creator>Jim Higson</dc:creator>
		<pubDate>Sat, 02 May 2009 10:06:45 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-982</guid>
		<description>Hi, I found this in Google and made a Smarty (see smarty.net) plugin out of it in about twenty seconds:

Thanks.

&lt;?php

/*	Taken from http://recurser.com/articles/2008/03/11/format-json-with-php/ */

/**
 * Indents a flat JSON string to make it more human-readable
 *
 * @param string $json The original JSON string to process
 * @return string Indented version of the original JSON string
 */
function smarty_modifier_formatjson($json) {
 
    $result    = &#039;&#039;;
    $pos       = 0;
    $strLen    = strlen($json);
    $indentStr = &#039;  &#039;;
    $newLine   = &quot;\n&quot;;
 
    for($i = 0; $i &lt;= $strLen; $i  ) {
        
        // Grab the next character in the string
        $char = substr($json, $i, 1);
        
        // If this character is the end of an element, 
        // output a new line and indent the next line
        if($char == &#039;}&#039; &#124;&#124; $char == &#039;]&#039;) {
            $result .= $newLine;
            $pos --;
            for ($j=0; $j&lt;$pos; $j  ) {
                $result .= $indentStr;
            }
        }
        
        // Add the character to the result string
        $result .= $char;
 
        // If the last character was the beginning of an element, 
        // output a new line and indent the next line
        if ($char == &#039;,&#039; &#124;&#124; $char == &#039;{&#039; &#124;&#124; $char == &#039;[&#039;) {
            $result .= $newLine;
            if ($char == &#039;{&#039; &#124;&#124; $char == &#039;[&#039;) {
                $pos   ;
            }
            for ($j = 0; $j </description>
		<content:encoded><![CDATA[<p>Hi, I found this in Google and made a Smarty (see smarty.net) plugin out of it in about twenty seconds:</p>
<p>Thanks.</p>
<p>&lt;?php</p>
<p>/*	Taken from <a href="http://recurser.com/articles/2008/03/11/format-json-with-php/" rel="nofollow">http://recurser.com/articles/2008/03/11/format-json-with-php/</a> */</p>
<p>/**<br />
 * Indents a flat <span class="caps">JSON</span> string to make it more human-readable<br />
 *<br />
 * <code>param string $json The original JSON string to process<br />
 * </code>return string Indented version of the original <span class="caps">JSON</span> string<br />
 */<br />
function smarty_modifier_formatjson($json) {</p>
<p>    $result    = &#8216;&#8217;;<br />
    $pos       = 0;<br />
    $strLen    = strlen($json);<br />
    $indentStr = &#8216;  &#8216;;<br />
    $newLine   = &#8220;\n&#8221;;</p>
<p>    for($i = 0; $i &lt;= $strLen; $i  ) {</p>
<p>        // Grab the next character in the string<br />
        $char = substr($json, $i, 1);</p>
<p>        // If this character is the end of an element,<br />
        // output a new line and indent the next line<br />
        if($char  &#8216;}&#8217; || $char  &#8216;]&#8217;) {<br />
            $result .= $newLine;<br />
            $pos &#8212;;<br />
            for ($j=0; $j&lt;$pos; $j  ) {<br />
                $result .= $indentStr;<br />
            }<br />
        }</p>
<p>        // Add the character to the result string<br />
        $result .= $char;</p>
<p>        // If the last character was the beginning of an element,<br />
        // output a new line and indent the next line<br />
        if ($char  &#8216;,&#8217; || $char  &#8216;{&#8217; || $char  &#8216;[&#8216;) {<br />
            $result .= $newLine;<br />
            if ($char  &#8216;{&#8217; || $char == &#8216;[&#8217;) {<br />
                $pos   ;<br />
            }<br />
            for ($j = 0; $j</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dave</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-927</link>
		<dc:creator>dave</dc:creator>
		<pubDate>Tue, 30 Dec 2008 02:32:09 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-927</guid>
		<description>No problem, glad you found it useful!</description>
		<content:encoded><![CDATA[<p>No problem, glad you found it useful!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Resat</title>
		<link>http://recurser.com/articles/2008/03/11/format-json-with-php/comment-page-1/#comment-925</link>
		<dc:creator>Resat</dc:creator>
		<pubDate>Mon, 29 Dec 2008 20:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://recurser.com/articles/2008/03/11/format-json-with-php/#comment-925</guid>
		<description>Thank you man. This function very simple &amp; usefull. Now i can easely read server json responses.</description>
		<content:encoded><![CDATA[<p>Thank you man. This function very simple &amp; usefull. Now i can easely read server json responses.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
