JSON数据未显示

时间:2012-05-22 20:38:27

标签: jquery json

我正在使用csv文件中的数据来使用JSON显示它。我可以在警报中看到它,但不能在html中看到它。此外,我尝试创建一个关联数组,以便通过Jquery更容易引用,但它没有以这种方式传递。

 <?php 
 if (($handle = fopen('upload/05-22-2012-1-43-28BEN-new.csv'. '', "r")) !== FALSE) {
       while (($row_array = fgetcsv($handle, 1024, ","))) {
            while ($val != '') {
                foreach ($row_array as $key => $val) {
                        $row_array[] = array_combine($key, trim(str_replace('"', '', $val)));
                        }
                }
            $complete[] = $row_array;
            //print_r($row_array);

            }
            fclose($handle);
        }
        echo json_encode($complete);

 ?>

HTML

 <body>    
      <div id="showdata"></div>

 </body>

JQuery的

 $(document).ready(function(){
    $.getJSON('WF-XML.php', function(data) {
        alert(data); //uncomment this for debug
        //alert (data.item1+" "+data.item2+" "+data.item3); //further debug
        $('#showdata').html(data);
    });
});

结果

 [["11704","1611704","BENV1072"],["11703","1611703","BENV1073"]]

1 个答案:

答案 0 :(得分:1)

首先,您需要使用JSON.stringify对对象进行字符串化,同时.html()方法不会转义字符串 - 请尝试使用.text()。

我为测试创建了一个简单的小提琴 - http://jsfiddle.net/E9KTy/