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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区