从JSON数据中获取值[对象]

时间:2018-07-14 02:21:26

标签: javascript c# json

debugging screenshot

$.ajax({
    url: postUrl,
    type: 'GET',
    dataType: 'json',
    data: { "id": num },
    contentType: "application/json; charset=utf-8",
    success: function (data) {


        $.each(data, function (id, allFollowers) {
            result += 'Title : ' + **data** + '<br/>';
        });
我试过了:data.allFollowers [0] .screeName,数据[0] .allFollowers [0] .screeName ...

我可以在调试模式下看到这些值,但是它返回空错误。?

2 个答案:

答案 0 :(得分:0)

我认为您的错误与使用$.each函数有关。您应该使用以下代码来引用数据:

    $.each(data, function (id, follower) {
        result += 'Title : ' + follower + '<br/>';
    });

有关更多信息,请参阅此jQuery API Documentation

答案 1 :(得分:0)

根据您的屏幕截图,我假设数据看起来像这样:

//data is an object, with an array of allFollowers objects
var data = {
  "allFollowers": [{
      "AlternateText": "no photo",
      "profileImage": "http://foo.com/foo",
      "screenName": "foo",
      "userID": 15785100
    },
    {
      "AlternateText": "no photo",
      "profileImage": "http://bar.com/bar",
      "screenName": "bar",
      "userID": 12345678
    }

  ]
};

您仅在data上/而不是在其子数组(allFollowers)上进行迭代。因此,您必须更深入一点:

$.each(data, function(key, obj) {   
  $.each(obj, function(i, value){
    console.log("screen name %i: %o, User ID: %o", i, value.screenName, value.userID);
  })
})

控制台:

screen name 0: "foo", User ID: 15785100
screen name 1: "bar", User ID: 12345678

Hth ...