George_Fal 2011-09-05 20:07 采纳率: 0%
浏览 129

JavaScript JSON对象解码

I am new to JSON and AJAX and was hoping someone could help me out here.

Using an AJAX request to a PHP script, I get some data from SQL database -> encoded as JSON string and sent it back to the calling javascript.

This is the data i get back

Array[{"categoryName":"Apartments For Rent"},{"categoryName":"Apartments For Sale"},{"categoryName":"Room For Rent (Shared)"},{"categoryName":"Paying Guest"},{"categoryName":"Office\/Shop\/Commercial Space"},{"categoryName":"Land"},{"categoryName":"Parking Spots"},{"categoryName":"Other"}]

How can I get the individual 'CategoryName' values from this (in a while loop)?

EDIT : PHP CODE THAT SENDS THE JSON DATA

    $arr = array();
    while($row = mysql_fetch_array($temp,MYSQL_ASSOC))
    {
        $arr[]=$row;
    }
    echo json_encode($arr);

Thank you for the help
Ankit

  • 写回答

3条回答 默认 最新

  • weixin_33695082 2011-09-05 20:09
    关注

    You could loop through the array and access individual elements by their index:

    for (var i = 0; i < data.length; i++) {
        alert(data[i].categoryName);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程