<?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>[MeIsProgrammer] &#187; PHP</title>
	<atom:link href="http://just-thor.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://just-thor.com</link>
	<description>all play and no work makes me a dull boy</description>
	<lastBuildDate>Sat, 03 Jul 2010 18:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP communicate with Java using JSON &#8211; Unicode Version</title>
		<link>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/</link>
		<comments>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 12:30:13 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=71</guid>
		<description><![CDATA[JSON is one of the most popular message exchange format nowadays used in web application.Most of the time,we used JSON as a communication message format between Javascript and Server side [...]]]></description>
			<content:encoded><![CDATA[<p>JSON is one of the most popular message exchange format nowadays used in web application.Most of the time,we used JSON as a communication message format between Javascript and Server side language,such as PHP.We can also use it to exchange messages between different languages.</p>
<p>For instance,we want to store a PHP array in database and later to be processed by Java,we can do something like</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;&quot;value1&quot;,&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue)
//we will get something like {&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}
//and then, we store the value to db
</pre></pre></p>
<p>On the other hand,we use Java  to read it and decode it.</p>
<p><pre><pre class="brush: java">
//read from db and got the resultset,assuming we are using JDBC
while (rs.next()){
String rawValue = rs.getString(&quot;encodedValue&quot;);
// since we encoded an associative array,it will be translated to a Java Map object instance.
// we are using JSON-lib for JSON parsing the following example
JSONObject json = JSONObject.fromObject(jsonString);
Map decodedValue = (Map) JSONObject.toBean(json, Map.class);
String value1 = decodedValue.get(&quot;key1&quot;);
}
</pre></pre></p>
<p>What if we are trying to store unicode value using JSON format in PHP?<br />
For example,</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;&quot;你好吗&quot;,&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue);
//This is what we get after encoded
//{&quot;key1&quot;:&quot;\u4f60\u597d\u5417&quot;,&quot;key2&quot;:&quot;value2&quot;}
</pre></pre></p>
<p>If you decoded it back to Java,key1 value will return &#8220;u4f60u597du5417&#8243; instead.This is so wrong.</p>
<p>How to solve it?<br />
We can use <b>mb_convert_encoding</b> function in PHP.<br />
Let&#8217;s make some changes to our code.</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;mb_convert_encoding(&quot;你好吗&quot;, &quot;UTF-8&quot;),&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue);
</pre></pre></p>
<p>By doing so,values are stored in database in unicode format,and you can decode it back in Java.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
