簡體   English   中英

在JavaScript中讀取包含許多對象的JSON輸出

[英]Reading a JSON output in JavaScript with a lot of 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);

但是我似乎無法達到目標?

“業務”是指一個數組(方括號),因此element.Business.userpic不存在(盡管element.Business [0] .userpic存在)。 根據您要實現的目標,您將不得不遍歷Business或訪問特定數組項的用戶圖片。

您的業​​務對象是對象數組

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

檢查此JSFiddle腳本以了解其內容

樣品輸出

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

這將幫助您

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

您的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

第二種方法可能更好,因為如果您更改陣列的順序或在陣列中添加新項等不會中斷,並且使用映射提供的性能提升可能不足以證明增加的維護成本。

我只是使用您的示例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)
        })
     }
   })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM