json格式

代码
<?php
   
// Database credentials
   /*$host = 'internal-db.s83073.gridserver.com'; 
   $db = 'db83073_gh'; 
   $uid = 'db83073'; 
   $pwd = '8ZdzVHDy';
*/
    $db 
= 'gohour_demo'
   $uid 
= 'gohour_demo'
   $pwd 
= 'S3nK4vbD8g';
     
// Connect to the database server   
    $link = mysql_connect($host, $uid, $pwd) or die("Could not connect");
   mysql_query(
"SET NAMES utf8");
   
//select the json database
   mysql_select_db($db) or die("Could not select database");
   
   
// Create an array to hold our results
   $arr = array();
   
//Execute the query
   $rs = mysql_query("SELECT Name FROM country_state_city where ParentID=542");
   
   
// Add the rows to the array 
   while($obj = mysql_fetch_object($rs)) {
   $arr[] 
= $obj;
   }
   
  
// return the json result. The string country_state_city is just a name for the container object. Can be set anything.
   echo '{"country_state_city":'.json_encode($arr).'}';?>

php代码

与之对应的json

{"country_state_city":[{"Name":"\u9999\u6e2f\u5cf6"},{"Name":"\u4e5d\u9f8d"},{"Name":"\u65b0\u754c"}]}

posted on 2010-04-26 15:06  fairycao  阅读(208)  评论(0)    收藏  举报