dongluo6343 2013-04-04 01:00
浏览 54
已采纳

在jQuery中输出json数组

I am a little stuck on how to retrieve all the values from the json array in jquery. I know it needs to be looped but how ? The function - at the moment is:

$.ajax({                                      
    url: 'query.php',                     
    data: "",                       
    dataType: 'json',                   
    success: ajaxfunction
   }); 

function ajaxfunction(json_data){
// problem is below, i need to output all the data from the array         
    console.log (json_data)
    while(json_data){
    $('#maindisplay').html("<b>Product: </b>"+json_data.prod_name+"<b> colour: </b>"+json_data.colour); // problem is here, i need to output all the data from the array
    }
  } 

The php:

$result = mysql_query("SELECT * FROM fproduct WHERE fproduct.category='Shirts'");
$elements = array ();
do{
   $row=mysql_fetch_array($result); 
    if ($row)    //if there is anything
              $elements[] = ($row); 

} while($row);
echo json_encode($elements);
  • 写回答

1条回答 默认 最新

  • duanke2012 2013-04-04 01:03
    关注

    You have to iterate over the elements of the array.

    function ajaxfunction(json_data){
        for (var i = 0; i < json_data.length; i++){
            $('#maindisplay').append($("<b>Product: </b>"+json_data[i].prod_name+"<b> colour: </b>"+json_data[i].colour+"<br>"));
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序