dongse7261 2010-01-06 22:36
浏览 62
已采纳

PHP json_encode结果和jQuery

I'm trying to take a jSON encoded string out of my database and loop through the items but I'm having some difficulty. Here's the string in the database:

["volunteers","seat_dedication_program","memberships"]

And here is the code:

//Looks for _checkbox when looping through my database fields (object dbVals) and turns it into a true jQuery array if it finds it.
if( key.search(/_checkbox/i) > 0 ) var arr = $.makeArray(dbVals[key]);

//If it is an array, loop through the array values and show them
if($.isArray(arr)==true){
    $.each(arr, function(i, n){
        alert(i + " : " + n);
    });
}

What I want is this:

//alert
0 : volunteers
//alert
1 : seat_dedication_program etc...

What I'm getting is this:

//alert
0 : ["volunteers","seat_dedication_program","memberships"]

I think I've included all relevant data. Can anyone help me figure out why this is happening?

Thanks.

  • 写回答

2条回答 默认 最新

  • dongnan1989 2010-01-06 22:45
    关注

    Using $.makeArray(..) is giving you an array where the only element is the string you gave it. You need to parse the string into a JavaScript object. Use the JSON2.js library to parse then your code would look something like this.

    var arr = JSON.parse(dbVals[key]);
    
    if($.isArray(arr)==true){
        $.each(arr, function(i, n){
            alert(i + " : " + n);
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?