求助 JSON问题
是这样的,服务端返回的是一个二维数组数据,在js端 我接收到数据,但是
汉字是乱码?
出输的html是错误的,循环不出数据来??
还请高手指点一下。
js:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> html='<table>';
var i,size,result;
url='stock.php';
$.post(url,
function(data){
result = eval('(' + data + ')');
size = result.length;
for (i = 1; i < size; i++) {
html += "<tr><td>" + result[i][1] + "</td><td>" + result[i][2]+ "</td><td>" + result[i][3] + "</td></tr>";
}
});
html+='</table>';
alert(html);
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><?php
$url = 'http://data.stock.hexun.com/quotes/stock_4.htm';
$content = @file_get_contents($url);
preg_match('/<table width=\"100%\"[^>]*>(.*)<\/table>/isU', $content,$str);
preg_match_all('/<tr>(.*)<\/tr>/isU', $str[0],$strs);
foreach ($strs[0] as $value){
//$pingyings = str_replace('__', '', $pingying);
preg_match_all('/(<td[^>]*>|<th[^>]*>)(.*)(<\/td>|<\/th>|<\/div>)/isU', $value,$strs);
$zhishu[] = $strs[2];
}
for($i=1,$size=count($zhishu); $i<$size; $i++){
$zhishu[$i][0] = str_replace('<div>', '', $zhishu[$i][0]);
//$zhishu[$i][0] = iconv('gbk', 'utf-8', $zhishu[$i][0]);
}
die(JSON($zhishu));
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
}
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
<?php header("Content-type:text/html;charset=gbk");
$url = 'http://data.stock.hexun.com/quotes/stock_4.htm';
...
...
...
?><br><font color="#e78608">------解决方案--------------------</font><br>按你的数据,只能是一维数组<br>你的循环错误在于<br>1、起点错了,数组下标从 0 开始,你却从 1<br>2、明明是一维数组,你却当做二维来处理,自然就文不对题了<br><br>数据是三个一组的<br>for (i = 0; i html += "<tr>
<td>" + result[i] + "</td>
<td>" + result[i+1]+ "</td>
<td>" + result[i+2] + "</td>
</tr>";<br>}<br><br><font color="#e78608">------解决方案--------------------</font><br>用php组成2维的不就行了 <div class="clear"></div>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号