2014-03-26 53 views
0

我的功能在以下格式返回JSON數據:如何計算JSON數據中唯一鍵值對的數量?

"searchResult":[ 
     { 
      "@count":"100", 
      "item":[ 
       { 
       "itemId":[ 
        "151258132867" 
       ], 
       "title":[ 
        "Apple iPhone 4 - clean esn - Black (Verizon) Smartphone" 
       ], 
       "globalId":[ 
        "EBAY-US" 
       ], 
       "primaryCategory":[ 
        { 
         "categoryId":[ 
          "9355" 
         ], 
         "categoryName":[ 
          "Cell Phones & Smartphones" 
         ] 
        } 
       ], 
       "galleryURL":[ 
        "http:\/\/thumbs4.ebaystatic.com\/m\/mGwHzpyvkph9-nU12sCspxw\/140.jpg" 
       ], 
       "viewItemURL":[ 
        "http:\/\/www.ebay.com\/itm\/Apple-iPhone-4-clean-esn-Black-Verizon-Smartphone-\/151258132867?pt=Cell_Phones" 
       ], 
       "productId":[ 
        { 
         "@type":"ReferenceID", 
         "__value__":"101787954" 
        } 
       ], 
       "paymentMethod":[ 
        "PayPal" 
       ], 
       "autoPay":[ 
        "false" 
       ], 
       "postalCode":[ 
        "93905" 
       ], 
       "location":[ 
        "Salinas,CA,USA" 
       ], 
       "country":[ 
        "US" 
       ], 
       "shippingInfo":[ 
        { 
         "shippingServiceCost":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"0.0" 
          } 
         ], 
         "shippingType":[ 
          "Free" 
         ], 
         "shipToLocations":[ 
          "US" 
         ], 
         "expeditedShipping":[ 
          "true" 
         ], 
         "oneDayShippingAvailable":[ 
          "false" 
         ], 
         "handlingTime":[ 
          "2" 
         ] 
        } 
       ], 
       "sellingStatus":[ 
        { 
         "currentPrice":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"96.0" 
          } 
         ], 
         "convertedCurrentPrice":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"96.0" 
          } 
         ], 
         "bidCount":[ 
          "17" 
         ], 
         "sellingState":[ 
          "Active" 
         ], 
         "timeLeft":[ 
          "P0DT0H0M11S" 
         ] 
        } 
       ], 
       "listingInfo":[ 
        { 
         "bestOfferEnabled":[ 
          "false" 
         ], 
         "buyItNowAvailable":[ 
          "false" 
         ], 
         "startTime":[ 
          "2014-03-19T18:29:51.000Z" 
         ], 
         "endTime":[ 
          "2014-03-26T18:29:51.000Z" 
         ], 
         "listingType":[ 
          "Auction" 
         ], 
         "gift":[ 
          "false" 
         ] 
        } 
       ], 
       "returnsAccepted":[ 
        "false" 
       ], 
       "condition":[ 
        { 
         "conditionId":[ 
          "3000" 
         ], 
         "conditionDisplayName":[ 
          "Used" 
         ] 
        } 
       ], 
       "isMultiVariationListing":[ 
        "false" 
       ], 
       "topRatedListing":[ 
        "false" 
       ] 
       }, 
       { 
       "itemId":[ 
        "360810145746" 
       ], 
       "title":[ 
        "U Apple iPhone 3GS - 8GB - Black (FACTORY UNLOCKED) Smartphone (C)" 
       ], 
       "globalId":[ 
        "EBAY-US" 
       ], 
       "subtitle":[ 
        "****USA SELLER*****FAST SHIPPING*****TOP SELLER****" 
       ], 
       "primaryCategory":[ 
        { 
         "categoryId":[ 
          "9355" 
         ], 
         "categoryName":[ 
          "Cell Phones & Smartphones" 
         ] 
        } 
       ], 
       "galleryURL":[ 
        "http:\/\/thumbs3.ebaystatic.com\/m\/mQGXy5hucEtqk6inUKFu67g\/140.jpg" 
       ], 
       "viewItemURL":[ 
        "http:\/\/www.ebay.com\/itm\/U-Apple-iPhone-3GS-8GB-Black-FACTORY-UNLOCKED-Smartphone-C-\/360810145746?pt=Cell_Phones" 
       ], 
       "productId":[ 
        { 
         "@type":"ReferenceID", 
         "__value__":"99984545" 
        } 
       ], 
       "paymentMethod":[ 
        "PayPal" 
       ], 
       "autoPay":[ 
        "true" 
       ], 
       "postalCode":[ 
        "10001" 
       ], 
       "location":[ 
        "New York,NY,USA" 
       ], 
       "country":[ 
        "US" 
       ], 
       "shippingInfo":[ 
        { 
         "shippingServiceCost":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"0.0" 
          } 
         ], 
         "shippingType":[ 
          "Free" 
         ], 
         "shipToLocations":[ 
          "US", 
          "CA", 
          "GB", 
          "AU", 
          "FR", 
          "HK", 
          "MT", 
          "IE", 
          "IL", 
          "NZ", 
          "PH", 
          "SG" 
         ], 
         "expeditedShipping":[ 
          "true" 
         ], 
         "oneDayShippingAvailable":[ 
          "false" 
         ], 
         "handlingTime":[ 
          "1" 
         ] 
        } 
       ], 
       "sellingStatus":[ 
        { 
         "currentPrice":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"84.95" 
          } 
         ], 
         "convertedCurrentPrice":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"84.95" 
          } 
         ], 
         "sellingState":[ 
          "Active" 
         ], 
         "timeLeft":[ 
          "P12DT0H9M11S" 
         ] 
        } 
       ], 
       "listingInfo":[ 
        { 
         "bestOfferEnabled":[ 
          "false" 
         ], 
         "buyItNowAvailable":[ 
          "false" 
         ], 
         "startTime":[ 
          "2013-12-08T18:33:51.000Z" 
         ], 
         "endTime":[ 
          "2014-04-07T18:38:51.000Z" 
         ], 
         "listingType":[ 
          "StoreInventory" 
         ], 
         "gift":[ 
          "false" 
         ] 
        } 
       ], 
       "returnsAccepted":[ 
        "true" 
       ], 
       "condition":[ 
        { 
         "conditionId":[ 
          "3000" 
         ], 
         "conditionDisplayName":[ 
          "Used" 
         ] 
        } 
       ], 
       "isMultiVariationListing":[ 
        "false" 
       ], 
       "discountPriceInfo":[ 
        { 
         "originalRetailPrice":[ 
          { 
          "@currencyId":"USD", 
          "__value__":"449.0" 
          } 
         ], 
         "pricingTreatment":[ 
          "STP" 
         ], 
         "soldOnEbay":[ 
          "false" 
         ], 
         "soldOffEbay":[ 
          "false" 
         ] 
        } 
       ], 
       "topRatedListing":[ 
        "true" 
       ] 
       } 

等每一個項目落在特定的categoryId下。因此,例如在這種情況下,我想要做的是統計每個唯一的categoryId出現的次數,然後將前兩個categoryIds存儲在數組中。我認爲這可以通過For循環完成,但我不確定如何設置。任何幫助表示讚賞,謝謝!

+0

你說「每一個項目落在特定的categoryId下」,但你只顯示一個包含類別ID的對象。你可能想更清楚。這些都會在層次結構中處於同一級別嗎?他們會嵌套嗎? –

+0

我已經在代碼中添加了第二項結果,以顯示嵌套的外觀。讓我知道如果你需要我添加其他東西。 – KoftaClarence

回答

0

一個哈希categoryId s這個應該工作:

var o = {}; 
data.searchResult[0].item.forEach(function(item) { 
    var id = item.primaryCategory[0].categoryId; 
    if (o[id]) o[id]++; 
    else o[id] = 1; 
}); 
var top2 = Object.keys(o).sort(function(a, b) { return o[b]-o[a]; }).slice(0, 2); 
console.log('Top two categories: ' + top2.join(', ')); 
+0

大部分這是有道理的,你能解釋一下這裏發生了什麼嗎?: '{return o [b] -o [a]; })。slice(0,2); console.log('Top two categories:'+ top2.join(','))' – KoftaClarence

+0

按降序出現的次數對類別ID進行排序,關閉前兩個類別並將其打印到控制檯。 –

+0

太棒了,謝謝! – KoftaClarence

0

遍歷searchResult.item並存儲在<CategoryID, NumOccurences>

var categoryIds = {}; 
for (var i = 0, l = searchResult.item.length; i < l; i++) { 
    var categoryId = searchResult.item[i].primaryCategory.categoryId; 
    categoryIds[categoryId] = ++(categoryIds[categoryId] || 0); 
} 

// console.log(categoryIds); 
// > { '1234': 1, '2345': 2, ... } 
+0

糾正我,如果我錯了,但不應該它searchResult.length,所以它遍歷searchResult內的所有項目?我收到一個錯誤,指出searchResult在我嘗試運行它時未定義,不確定是否導致它。 – KoftaClarence

+0

來自您發佈的JSON,它看起來像'searchResult.item'是項目的數組。如果它是不同的對象,只需替換你的數組的'searchResult.item'。 – kavun

相關問題