Home  >  Article  >  Backend Development  >  JSON有关问题

JSON有关问题

WBOY
WBOYOriginal
2016-06-13 13:42:31856browse

求助 JSON问题
是这样的,服务端返回的是一个二维数组数据,在js端 我接收到数据,但是

汉字是乱码?
出输的html是错误的,循环不出数据来??

还请高手指点一下。


js:

JScript code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->        html='
'; var i,size,result; url='stock.php'; $.post(url, function(data){ result = eval('(' + data + ')'); size = result.length; for (i = 1; i "; } }); html+='
" + result[i][1] + " " + result[i][2]+ " " + result[i][3] + "
'; alert(html);




服务端:
PHP code
<!--

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%\"[^>]*>(.*)/isU', $content,$str);
preg_match_all('/<tr>(.*)/isU', $str[0],$strs);
foreach ($strs[0] as $value){
    //$pingyings = str_replace('__', '', $pingying);
    preg_match_all('/(<td>]*>|</td>
<th>]*>)(.*)(||)/isU', $value,$strs);
    $zhishu[] = $strs[2];
}

for($i=1,$size=count($zhishu); $i', '', $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);
}
<br><br><br><font color="#e78608">------解决方案--------------------</font><br>你返回给 js 的是这样的字符串<br><br>[["股市名称","最新","涨跌幅%"],["道琼斯","13194.10","+0.12%"],["纳斯达克","3040.73","+0.03%"],["标普500","1394.28","-0.12%"],["法国CAC40","3564.51","+0.40%"],["日经225","10123.06","+0.72%"],["英国富时","5945.43","-0.18%"],["澳洲全股","4366.90","-0.20%"]]<br><br>因为他的格式不符合JavaScript对象符号(JSON)的规则,所以只能被 eval 解析成一维数组(js没有多维数组的概念)<br><br>接下来的事情就不必我再唠叨了吧
<br><font color="#e78608">------解决方案--------------------</font><br>#1.php端加返回头指定编码为gb2312或者直接gbk<br><dl class="code">PHP code<pre class="brush:php;toolbar:false">
<?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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn