遍历json数组jquery [英] loop through json array jquery

查看:103
本文介绍了遍历json数组jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历此过程以获取名称"值.这是我目前所拥有的,但似乎没有用,从此处发布的内容中尝试了一些其他方法,但似乎无济于事.

I'm trying to loop through this to get the 'name' values. This is what I currently have, but it doesn't seem to be working, tried a few others from what was posted here but nothing seemed to work.

    $.get("/get_names", {campaign_id: $('select[name="id"]').val()}, 
                function(data){
                    $.each(data, function(i, item) {
                        alert(item);
                    });
                }
   );

Json被退回:

   [
           {
              "name":"age"
           },
           {
              "name":"asdf"
           },
           {
              "name":"drivername"
           },
           {
              "name":"drivers"
           },
           {
              "name":"firstname"
           },
           {
              "name":"gender"
           },
           {
              "name":"lastname"
           },
           {
              "name":"make"
           },
           {
              "name":"model"
           },
           {
              "name":"vehicles"
           },
           {
              "name":"year"
           }
        ]

我尝试使用:

item.name
item[i].name

有什么建议吗?

谢谢!

推荐答案

您必须将字符串解析为JSON(data[0] == "["表示data实际上是字符串,而不是对象):

You have to parse the string as JSON (data[0] == "[" is an indication that data is actually a string, not an object):

data = $.parseJSON(data);
$.each(data, function(i, item) {
    alert(item);
});

这篇关于遍历json数组jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆