weixin_33714884 2015-08-13 20:16 采纳率: 0%
浏览 41

发布解析JSON

I'm getting a JSON object (I think) back from my AJAX call like this:

$.ajax({
   url: 'testing.php',
            type: 'POST',
            data: {
                code: code_got,
            },
            success: function(data) {
              var list_data = data;
              console.log(list_data);//this is the object shown below
        var list_name = list_data['data']['name'];
        console.log(list_name);//this throws an error that `name` is undefined
            }
          });
});

The response looks like this:

 {
  "total" : 5,
  "data" : [
    {
      "id" : "312464ee1e",
      "stats" : {
        "members" : 58,
      },
      "web_id" : 11966492,
      "name" : "List 1"
    },
    {
      "id" : "312464ee1e",
      "stats" : {
        "members" : 123,
      },
      "web_id" : 1196649,
      "name" : "List 2"
    },

  ]
}

I'm trying to get (first off) the first list name. I've tried this, but it doesn't work:

var list_data = data;
console.log(list_data);//this is the object shown above
var list_name = list_data['data']['name']; //this throw an error "Uncaught TypeError: Cannot read property 'name' of undefined"

Second, I want to do an each to get all of the name items from the data, but I can't get to this until I figure out the JSON structure.

What am I doing wrong here?

  • 写回答

5条回答 默认 最新

  • 北城已荒凉 2015-08-13 20:20
    关注

    You are looking for an object property on an array list_data['data']:

    you want a property of an object within that array

    Try

    var list_name = list_data['data'][0]['name'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探