|
PHP复制代码 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
//建立数据库连接
//$con = mysql_connect ( "localhost", "name", "yourpwd" );
if (! $con) {
die ( 'Could not connect: ' . mysql_error () );
}
//选择查询的数据库
mysql_select_db ( "china_city", $con );
//设置字符集为UTF-8
mysql_query ( "set names utf8" );
$query = "select * from provincial";
//执行SQL语句
$result = mysql_query ( $query );
//循环 将查询的数据存入数组
while ( $row = mysql_fetch_assoc ( $result ) ) {
$response [] = $row;
}
//使用Foreach遍历数组 同时使用urlencode处理 含有中文的字段
foreach ( $response as $key => $value ) {
$newData[$key] = $value;
$newData [$key] ['Provincial'] = urlencode ( $value ['Provincial'] );
}
echo urldecode ( json_encode ( $newData ) );
mysql_close ( $con );
?> 复制代码
最终JSON数据如下
[{"pid":"1","Provincial":"北京市"},{"pid":"2","Provincial":"天津市"},{"pid":"3","Provincial":"上海市"},
{"pid":"4","Provincial":"重庆市"},{"pid":"5","Provincial":"河北省"},{"pid":"6","Provincial":"山西省"},
{"pid":"7","Provincial":"台湾省"},{"pid":"8","Provincial":"辽宁省"},{"pid":"9","Provincial":"吉林省"},
{"pid":"10","Provincial":"黑龙江省"},{"pid":"11","Provincial":"江苏省"},{"pid":"12","Provincial":"浙江省"},{"pid":"13","Provincial":"安徽省"},{"pid":"14","Provincial":"福建省"},{"pid":"15","Provincial":"江西省"},{"pid":"16","Provincial":"山东省"},{"pid":"17","Provincial":"河南省"},{"pid":"18","Provincial":"湖北省"},{"pid":"19","Provincial":"湖南省"},{"pid":"20","Provincial":"广东省"},{"pid":"21","Provincial":"甘肃省"},{"pid":"22","Provincial":"四川省"},{"pid":"23","Provincial":"贵州省"},{"pid":"24","Provincial":"海南省"},{"pid":"25","Provincial":"云南省"},{"pid":"26","Provincial":"青海省"},{"pid":"27","Provincial":"陕西省"},{"pid":"28","Provincial":"广西壮族自治区"},{"pid":"29","Provincial":"西藏自治区"},
{"pid":"30","Provincial":"宁夏回族自治区"},{"pid":"31","Provincial":"新疆维吾尔自治区"},
{"pid":"32","Provincial":"内蒙古自治区"},{"pid":"33","Provincial":"澳门特别行政区"},
{"pid":"34","Provincial":"香港特别行政区"}]
|
评分
-
查看全部评分
|