使用大量对象在JavaScript中读取JSON输出

时间:2013-12-10 10:39:47

标签: javascript json javascript-objects

这是我的JSON输出:

[
  {
    "Business": [
      {
        "id": "5739"
      },
      {
        "userid": ""
      },
      {
        "name": "Ben Electric"
      },
      {
        "description": ""
      },
      {
        "address": ""
      },
      {
        "email": "*****@gmail.com"
      },
      {
        "phone2": "050*****88"
      },
      {
        "phone3": ""
      },
      {
        "mobile": "050****88"
      },
      {
        "opentimes": ""
      },
      {
        "services": ""
      },
      {
        "places": ""
      },
      {
        "logo": null
      },
      {
        "image": null
      },
      {
        "video": ""
      },
      {
        "owner_name": "Ben Brant"
      },
      {
        "owners": "1"
      },
      {
        "userpic": "http://graph.facebook.com/****/picture"
      },
      {
        "circle": "3"
      },
      {
        "fc": "0"
      },
      {
        "rating_friends": ""
      },
      {
        "rating_global": "3.3333"
      },
      {
        "advice": ""
      },
      {
        "subscription": "none"
      }
    ]
  },
  {
    "Business": [
      {
        "id": "5850"
      },
      {
        "userid": ""
      },
      {
        "name": "Bla Bla"
      },
      {
        "description": ""
      },
      {
        "address": ""
      },
      {
        "email": "*****@gmail.com"
      },
      {
        "phone2": ""
      },
      {
        "phone3": ""
      },
      {
        "mobile": "0*****995"
      },
      {
        "opentimes": ""
      },
      {
        "services": ""
      },
      {
        "places": ""
      },
      {
        "logo": null
      },
      {
        "image": null
      },
      {
        "video": ""
      },
      {
        "owner_name": "Ben VBlooo"
      },
      {
        "owners": "1"
      },
      {
        "userpic": "http://graph.facebook.com/******/picture"
      },
      {
        "circle": "3"
      },
      {
        "fc": "0"
      },
      {
        "rating_friends": ""
      },
      {
        "rating_global": "2.0000"
      },
      {
        "advice": ""
      },
      {
        "subscription": "none"
      }
    ]
  },
  {
    "Info": {
      "message": "No user for the business"
    }
  },
  {
    "OK": {
      "message": "By Circle"
    }
  }
]

我试图以这种方式获取javascript中的对象,但它不起作用,我应该遍历每个Business对象吗?有没有办法直接访问真实的数据对象?

以下是我正在尝试的内容:

   $.ajax({
    type: 'POST',
    url: 'BLABLA',
    data: { BLABLA },
    dataType: 'json',
    success: function( resp ) {
        if(resp.length == 0) {
        $('.searchol').append('<li>No results found.</li>');
          return;
        }
      $.each(resp, function(index, element) {
         $('.searchol').append('Users Picture: '+element.Business.userpic);

但我似乎无法找到对象?

5 个答案:

答案 0 :(得分:0)

“Business”是指一个数组(方括号),因此element.Business.userpic不存在(尽管存在element.Business [0] .userpic)。根据您想要实现的目标,您必须遍历Business或访问特定数组项的userpic。

答案 1 :(得分:0)

您的业务对象是一个对象数组

"Business": [
  {
    "id": "5850"
  },

检查JSFiddle script如何阅读

示例输出

Picture: undefined (index):192
Picture: http://graph.facebook.com/****/picture 

答案 2 :(得分:0)

这将帮助你

$.each(resp, function(index, element) {
         $('.searchol').append('Users Picture: '+element.Business["userpic"]);

答案 3 :(得分:0)

你的JSON很奇怪。而不是:

Business : [
  { id : 'id1' }
  { name : 'name1' }
]


Business[0].id // access id
Business[1].name // access name

你必须记住每个属性在数组中的位置(或在数组上循环以找到它),你应该有:

Business : {
  id : 'id1',
  name : 'name1'
}

Business.id // access id
Business.name // access name

如果您无法更改JSON,可以使用以下两种方法快速获取Business的属性:

var propMap = {
  id : 0,
  userid : 1,
  name : 2 // etc
}

function getBusinessProp(business, prop) {
  return business[propMap[prop]][prop];
}

// usage :
$('.searchol').append('Users Picture: '+ getBusinessProp(element.Business, 'userpic'));

如果您的数组可能缺少某些项目,或者每个项目的项目顺序不同,那么您需要迭代才能找到您感兴趣的属性:

function getBusinessProp(business, prop) {
  for (var i=0; i<business.length; i++) {
    if (business[i].hasOwnProperty(prop)) {
      return business[i][prop];
    }
  }
}

// same usage, no need for the map anymore

第二种方法可能更好,因为如果更改数组的顺序或在数组中添加新项目等不会中断,并且使用地图提供的性能提升可能不足以证明增加的维护成本。

答案 4 :(得分:0)

我刚刚使用你的样本json尝试了这段代码

$.each(resp, function(index,element){
   $.each(element, function(ind,ele){
     if(ele.length){
        $.each(ele,function(ind,ele){
          if(ele.userpic) 
           console.log(ele.userpic)
        })
     }
   })
})