Python从urllib请求中解析JSON

3
我有以下的json数据:
{
findItemsByKeywordsResponse: [
{
ack: [
"Success"
],
version: [
"1.12.0"
],
timestamp: [
"2012-08-20T21:42:08.532Z"
],
searchResult: [
{
@count: "2",
item: [
{
itemId: [
"380459520121"
],
title: [
"Lenovo ThinkPad T61 Core 2 Duo 2.2GHz 1GB 120GB DVD-RW WiFi Laptop 15" Notebook"
],
globalId: [
"EBAY-US"
],
primaryCategory: [
{
categoryId: [
"177"
],
categoryName: [
"PC Laptops & Netbooks"
]
}
],
galleryURL: [
"http://thumbs2.ebaystatic.com/pict/3804595201214040_1.jpg"
],
viewItemURL: [
"http://www.ebay.com/itm/Lenovo-ThinkPad-T61-Core-2-Duo-2-2GHz-1GB-120GB-DVD-RW-WiFi-Laptop-15-Notebook-/380459520121?pt=Laptops_Nov05"
],
paymentMethod: [
"PayPal",
"VisaMC",
"Discover"
],
autoPay: [
"false"
],
postalCode: [
"55114"
],
location: [
"Saint Paul,MN,USA"
],
country: [
"US"
],
shippingInfo: [
{
shippingType: [
"Calculated"
],
shipToLocations: [
"US"
],
expeditedShipping: [
"true"
],
oneDayShippingAvailable: [
"false"
],
handlingTime: [
"1"
]
}
],
sellingStatus: [
{
currentPrice: [
{
@currencyId: "USD",
__value__: "112.5"
}
],
convertedCurrentPrice: [
{
@currencyId: "USD",
__value__: "112.5"
}
],
bidCount: [
"5"
],
sellingState: [
"Active"
],
timeLeft: [
"P0DT0H18M18S"
]
}
],
listingInfo: [
{
bestOfferEnabled: [
"false"
],
buyItNowAvailable: [
"false"
],
startTime: [
"2012-08-15T22:00:26.000Z"
],
endTime: [
"2012-08-20T22:00:26.000Z"
],
listingType: [
"Auction"
],
gift: [
"false"
]
}
],
returnsAccepted: [
"true"
],
condition: [
{
conditionId: [
"3000"
],
conditionDisplayName: [
"Used"
]
}
],
isMultiVariationListing: [
"false"
]
},
{
itemId: [
"110931951761"
],
title: [
"Dell Latitude D630 Laptop Core Duo 1.8GHz 2GB Ram 60GB HDD Not Complete"
],
globalId: [
"EBAY-US"
],
primaryCategory: [
{
categoryId: [
"177"
],
categoryName: [
"PC Laptops & Netbooks"
]
}
],
galleryURL: [
"http://thumbs2.ebaystatic.com/pict/1109319517614040_2.jpg"
],
viewItemURL: [
"http://www.ebay.com/itm/Dell-Latitude-D630-Laptop-Core-Duo-1-8GHz-2GB-Ram-60GB-HDD-Not-Complete-/110931951761?pt=Laptops_Nov05"
],
paymentMethod: [
"PayPal"
],
autoPay: [
"false"
],
postalCode: [
"17737"
],
location: [
"Hughesville,PA,USA"
],
country: [
"US"
],
shippingInfo: [
{
shippingServiceCost: [
{
@currencyId: "USD",
__value__: "24.0"
}
],
shippingType: [
"Flat"
],
shipToLocations: [
"US"
],
expeditedShipping: [
"false"
],
oneDayShippingAvailable: [
"false"
],
handlingTime: [
"1"
]
}
],
sellingStatus: [
{
currentPrice: [
{
@currencyId: "USD",
__value__: "61.99"
}
],
convertedCurrentPrice: [
{
@currencyId: "USD",
__value__: "61.99"
}
],
bidCount: [
"9"
],
sellingState: [
"Active"
],
timeLeft: [
"P0DT0H8M7S"
]
}
],
listingInfo: [
{
bestOfferEnabled: [
"false"
],
buyItNowAvailable: [
"false"
],
startTime: [
"2012-08-17T21:50:15.000Z"
],
endTime: [
"2012-08-20T21:50:15.000Z"
],
listingType: [
"Auction"
],
gift: [
"false"
]
}
],
returnsAccepted: [
"true"
],
condition: [
{
conditionId: [
"3000"
],
conditionDisplayName: [
"Used"
]
}
],
isMultiVariationListing: [
"false"
]
}
]
}
],
paginationOutput: [
{
pageNumber: [
"1"
],
entriesPerPage: [
"2"
],
totalPages: [
"486815"
],
totalEntries: [
"973629"
]
}
],
itemSearchURL: [
"http://www.ebay.com/sch/i.html?_nkw=laptops&_ddo=1&_ipg=2&_pgn=1"
]
}
]
}

我尝试使用以下for循环获取“item”数据:
  data = json.load(urllib2.urlopen(url))
  #print data
  for item in data['findItemsByKeywordsResponse'][0]['searchResult'][0]['item']:
    for itemId in item['itemId']:
      print itemId

这个可以正常工作,但我不确定如何获取我试图在底部显示的所有项目数据。

[
{
@count: "2",
item: [
{},
{}
]
}
],
1个回答

0

data['findItemsByKeywordsResponse'] 是一个 list,所以你可以通过使用 the_list[0] 等方式来访问其中的项。

list 中没有键,键属于 dict


我很困惑,为什么如果一个项目是列表,即使我按照你说的那样做了 data['findItemsByKeywordsResponse']['searchResult'] ,我还是无法打印出来。但是我得到了同样的错误。 - add-semi-colons
1
我的错,实际上 data['findItemsByKeywordsResponse'] 本身是一个列表(从你的打印输出中可以看到)。我会相应地更新我的答案。 - Thomas Orozco

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接