Reputation: 23
I keep scratching my head why it is not printing json content I need. Anyone know what I'm doing wrong ?
This is the dictionary
> "listinginfo": {
> "438309609514180554": {
> "listingid": "438309609514180554",
> "price": 35,
> "fee": 4,
> "publisher_fee_app": 730,
> "publisher_fee_percent": "0.10000000149011612",
> "currencyid": "2003",
> "steam_fee": 1,
> "publisher_fee": 3,
> "converted_price": 50,
> "converted_fee": 7,
> "converted_currencyid": "2020",
> "converted_steam_fee": 2,
> "converted_publisher_fee": 5,
> "converted_price_per_unit": 50,
> "converted_fee_per_unit": 7,
> "converted_steam_fee_per_unit": 2,
> "converted_publisher_fee_per_unit": 5,
> "asset": {
> "currency": 0,
> "appid": 730,
> "contextid": "2",
> "id": "1579403640",
> "amount": "1",
> "market_actions": [
> {
The code + I need the values for the keys I want to print:
while 1:
r = requests.get(url, headers=headers)
listingInfoStr = r.content
result= ujson.loads(listingInfoStr)
listingInfoJson= result['listinginfo']
for listingdata in listingInfoJson:
print listingdata.get('listingId')
print listingdata.get('subTotal')
print listingdata.get('feeAmount')
print listingdata.get('totalPrice')
time.sleep(10)
Thank you for your time.
Upvotes: 0
Views: 5114
Reputation: 1199
I ran your code and it looks like listingInfoJson comes back as a dict not a list. So when you iterate over it it's just pulling the keys.
You're calling the .get method on a unicode object, which will give you an AttributeError. You can run this code in to different ways:
for listingdata in listingInfoJson:
print listingInfoJson[listingdata].get('listingid')
print listingInfoJson[listingdata].get('subTotal')
print listingInfoJson[listingdata].get('feeAmount')
print listingInfoJson[listingdata].get('totalPrice')
or the better way of (edited for comments):
if listingInfoJson:
for key, value in listingInfoJson.iteritems():
print value.get('listingid')
print value.get('subTotal')
print value.get('feeAmount')
print value.get('totalPrice')
else:
print "listingInfoJson is empty"
I'd also check your key values, listingId
should be listingid
Upvotes: 0
Reputation: 353
You may use requests.Response.json method to get parsed JSON:
r = requests.get(url, headers=headers)
listingInfoJson = r.json()['listinginfo']
Upvotes: 3