2016-04-21 204 views
-5

我是json解析的新手,但我想從此json響應中獲取salePrice,name,modelnumber。答案似乎全部在1行,我沒有任何運氣獲得數據。Python Json解析輸出

'{u'sort':u'relevance',u'items':[{u'marketplace':False,u'imageEntities':[{u'entityType':u'SECONDARY',u'thumbnailImage ':u'http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg',u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-3397/k2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg',u'mediumImage':u'http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363-b442-468e-8601-88273996cd30.v1.jpg'},{u'entityType':u'SECONDARY',u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2-_e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg',u 'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg',u'mediumImage':u'http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg'},{u'entityType':u'SECONDARY',u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg',u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg' ,'u'mediumImage':u'http://i5.walmartimages.com/dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg'},{u'entityType':u'SECONDARY',u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-1ba6/k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1.jpg',u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg',u'mediumImage':u' http://i5.walmartimages.com/dfw/dce07b8c-5d76/k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg'回溯(最近一次通話最後):{u'entityType':u'SECONDARY',u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg',u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-a419/k2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg',u'mediumImage':u'http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98-52e5-4461-8587-0007f84dee24.v1.jpg'},{u'entityType' :u'PRIMARY',u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',u'mediumImage':u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg'}],u'numReviews':4,u'productUrl': u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FThe-Evil-Within-Xbox-One%2F26096326%3Faffp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi',u'customerRatingImage':u'http://i2.walmartimages.com/i/CustRating/4_8.gif',u'addToCartUrl':u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D26096326%7C1%26affp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi',u'shortDescription':u「由Shinji Mikami開發 - 生化危機系列的創造者 - 以及有才華的團隊Tango Gameworks,The Evil Within體現了純粹生存恐怖的意義。當你試圖擺脫只存在於最可怕的世界中的扭曲的陰謀時,體驗一個令人不安的現實。面對扭曲的生物並體驗真正的恐怖,所有這一切都通過定製版本的ID Tech 5引擎實現的尖端照明和動畫加強。在調查可怕的大屠殺現場時,偵探塞巴斯蒂安卡斯特利亞諾斯和他的夥伴們遇到了一個神祕而強大的力量。在看到同夥被屠殺後,塞巴斯蒂安被伏擊並昏迷不醒。當他醒來時,他發現自己陷入了一個瘋狂的世界,那些可怕的生物在死者中間漫步。面對難以想象的恐怖,爲生存而戰,塞巴斯蒂安開始了一段可怕的旅程,以揭開這個邪惡力量的背後。「,u'customerRating':u'4.75',u'affiliateAddToCartUrl':'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart%252FaddToCart%253Fitems%253D26096326%257C1%2526affp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg',u'productTrackingUrl':u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FThe-Evil-Within-Xbox-One%252F26096326%253Faffp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',u'stock':u'Available',u'categoryNode':u'2636_1040579',u'giftOptions':{u'allowGiftWrap':False,u' allowGiftMessage':False,u'allowGiftReceipt':False},u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',u'parentItemId':26096326,u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',u'bundle':False,u'longDescription ':u「<ul> <li>純粹的生存恐怖迴歸 - 生死恐怖之父Shinji Mikami,重新引導了一場令人心寒的新遊戲,其中包含着令人困惑的敘述。當你探索遊戲的折磨世界時,緊張和焦慮會顯着增加。當你爲生存而鬥爭時,你將擁有有限的資源,並在恐怖和行動的完美結合中體驗到深深的恐懼。 </li > <li>殘酷的陷阱和扭曲的生物 - 面對難以想象的恐怖和殘酷的陷阱,因爲你爲了生存而戰勝壓倒性的勝算。通過使用相同的惡魔裝置來對付壓倒性的致命動物,從而使自己變得邪惡。 </li > <li>未知世界中的未知威脅 - 在一個扭曲身邊的世界中,神祕和邪惡的恐懼將會前行。走廊,牆壁,門和整個建築物實時變化,將玩家置於現實中,隨時隨地出現威脅。 </li > <li>恐怖的新面貌 - 高度製造的環境,令人恐懼的焦慮和錯綜複雜的故事編織在一起,創造出一個身臨其境的世界,讓玩家處於緊張的高度。擊敗無法克服的恐怖,並通過發現邪惡內心體驗終極快感。 < /李> <李> ESRB評級:成熟,血腥場面,強烈的語言,和激烈的暴力< /李> </UL >」,u'standardShipRate ':4.97,u'categoryPath':u'Video遊戲/視頻遊戲',u'itemId':26096326,u'name':u'The Evil Within(Xbox One)',u'upc':u'093155118539',u'modelNumber':u'093155118539',u'salePrice' :49.96,u'availableOnline':True,u'msrp':59.99}],u'facets':[],u'start':1,u'numItems':1,u'query':u'093155118539' ,u'totalResults ':1,u'responseGroup':u'base'}

這裏是我的代碼

import json 
import requests 


def getPrice(request): 
    try: 
     price = request.find('"49.96"') 
     print price 
     return price 
    except: 
     print'fail Price' 

def getData(): 
    request = requests.get('http://api.walmartlabs.com/v1/search?query=093155118539&format=json&apiKey=KEYSHOULDGOHERE') 
    data = request.json() 
    print data #where I get my output 
    price = getPrice(request) 

getData() 

鏈接到API文檔 - 我沒有扔掉API密鑰提供。 https://developer.walmartlabs.com/io-docs

+3

這不是JSON,這是一個Python字典。 –

+2

「我沒有任何運氣獲取數據」是什麼意思?你的具體挑戰是什麼? – Chris

+0

嘿,我不知道如何解析這些信息來提取我指定的數據。 – turtle02

回答

1

你需要知道解析它的響應的格式。我看到了,你用過requests.get()

您的GET請求

request = requests.get(...) 

request.json()將返回你的JSON響應的字典對象後。它看起來像(格式化後,used the codes like of @Alex):

{u'sort': u'relevance', u'items': [{u'marketplace': False, 
             u'imageEntities': [ 
              {u'entityType': u'SECONDARY', 
              u'thumbnailImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg', 
              u'largeImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-3397/k2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg', 
              u'mediumImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363-b442-468e-8601-88273996cd30.v1.jpg'}, 
              {u'entityType': u'SECONDARY', 
              u'thumbnailImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2-_e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg', 
              u'largeImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg', 
              u'mediumImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg'}, 
              {u'entityType': u'SECONDARY', 
              u'thumbnailImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg', 
              u'largeImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg', 
              u'mediumImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg'}, 
              {u'entityType': u'SECONDARY', 
              u'thumbnailImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-1ba6/k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1.jpg', 
              u'largeImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg', 
              u'mediumImage': 
               u'http://i5.walmartimages.com/dfw/dce07b8c-5d76/k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg' 
              }, { 
               u'entityType': u'SECONDARY', 
               u'thumbnailImage': 
                u'http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg', 
               u'largeImage': 
                u'http://i5.walmartimages.com/dfw/dce07b8c-a419/k2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg', 
               u'mediumImage': 
                u'http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98-52e5-4461-8587-0007f84dee24.v1.jpg'}, 
              {You have did that 
               u'entityType': u'PRIMARY', 
               u'thumbnailImage': 
                u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg', 
               u'largeImage': 
                u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg', 
               u'mediumImage': 
                u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg'}], 
             u'numReviews': 4, 
             u'productUrl': 
              u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FThe-Evil-Within-Xbox-One%2F26096326%3Faffp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi', 
             u'customerRatingImage': 
              u'http://i2.walmartimages.com/i/CustRating/4_8.gif', 
             u'addToCartUrl': 
              u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D26096326%7C1%26affp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi', 
             u'shortDescription': u"Developed by " 
                  u"Shinji Mikami " 
                  u"- creator of " 
                  u"the seminal " 
                  u"Resident Evil " 
                  u"series - and " 
                  u"the talented " 
                  u"team at Tango " 
                  u"Gameworks, " 
                  u"The Evil " 
                  u"Within " 
                  u"embodies the " 
                  u"meaning of " 
                  u"pure survival " 
                  u"horror. " 
                  u"Experience a " 
                  u"disturbing " 
                  u"reality as " 
                  u"you try to " 
                  u"break free " 
                  u"from warped " 
                  u"machinations " 
                  u"that could " 
                  u"only exist in " 
                  u"the most " 
                  u"horrifying " 
                  u"worlds. Face " 
                  u"twisted " 
                  u"creatures and " 
                  u"experience " 
                  u"true terror, " 
                  u"all " 
                  u"heightened by " 
                  u"cutting edge " 
                  u"lighting and " 
                  u"animation " 
                  u"made possible " 
                  u"by a " 
                  u"customized " 
                  u"version of " 
                  u"the id Tech 5 " 
                  u"engine. While " 
                  u"investigating " 
                  u"the scene of " 
                  u"a gruesome " 
                  u"mass murder, " 
                  u"Detective " 
                  u"Sebastian " 
                  u"Castellanos " 
                  u"and his " 
                  u"partners " 
                  u"encounter a " 
                  u"mysterious " 
                  u"and powerful " 
                  u"force. After " 
                  u"seeing the " 
                  u"slaughter of " 
                  u"fellow " 
                  u"officers, " 
                  u"Sebastian is " 
                  u"ambushed and " 
                  u"knocked " 
                  u"unconscious. " 
                  u"When he " 
                  u"awakens, " 
                  u"he finds " 
                  u"himself in a " 
                  u"deranged " 
                  u"world where " 
                  u"hideous " 
                  u"creatures " 
                 salePrice,name,modelnumber  u"wander among " 
                  u"the dead. " 
                  u"Facing " 
                  u"unimaginable " 
                  u"terror, " 
                  u"and fighting " 
                  u"for survival, " 
                  u"Sebastian " 
                  u"embarks on a " 
                  u"frightening " 
                  u"journey to " 
                  u"unravel " 
                  u"what's behind " 
                  u"this evil " 
                  u"force.", 
             u'customerRating': u'4.75', 
             u'affiliateAddToCartUrl': 
              u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart%252FaddToCart%253Fitems%253D26096326%257C1%2526affp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi', 
             u'mediumImage': 
              salePrice,name,modelnumbersalePrice,name,modelnumberu'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg', 
             u'productTrackingUrl': 
              u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FThe-Evil-Within-Xbox-One%252F26096326%253Faffp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi', 
             u'stock': u'Available', 
             u'categoryNode': u'2636_1040579', 
             u'giftOptions': { 
              u'allowGiftWrap': False, 
              u'allowGiftMessage': False, 
              u'allowGiftReceipt': False}, 
             u'largeImage': 
              u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg', 
             u'parentItemId': 26096326, 
             u'thumbnailImage': 
              u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg', 
             u'bundle': False, 
             u'longDescription': 
              u"&lt;ul&gt;&lt;li&gt;Pure " 
              u"Survival Horror Returns - " 
              u"Shinji Mikami, the father of " 
              u"survival horror, is back to " 
              u"direct a chilling new game " 
              u"wrapped in haunting narrative. " 
              u"Tension and anxiety heighten " 
              u"dramatically as you explore " 
              u"the game's tortured world. " 
              u"You'll have limited resources " 
              u"at your disposal as you fight " 
              u"for your survival and " 
              u"experience profound fear in " 
              u"this perfect blend of horror " 
              u"and " 
              u"action.&lt;/li&gt;&lt;li&gt;Brutal Traps and Twisted Creatures - Face unthinkable horrors and cruel traps as you struggle to survive against overwhelming odds. Turn evil against itself by using the same diabolical devices against overwhelming deadly creatures.&lt;/li&gt;&lt;li&gt;Unknown Threats in an Uncertain World - Mysterious and wicked fears loom ahead in a world that warps and twists around you. Corridors, walls, doors, and entire buildings change in real-time, ensnaring players in a reality where threats can appear at any time and from any direction.&lt;/li&gt;&lt;li&gt;The New Face of Horror - Highly-crafted environments, horrifying anxiety, and an intricate story weave together to create an immersive world that will bring players to the height of tension. Defeat insurmountable terror and experience the ultimate thrill by discovering The Evil Within.&lt;/li&gt;&lt;li&gt;ESRB Rating: MATURE with Blood and Gore, Strong Language, and Intense Violence&lt;/li&gt;&lt;/ul&gt;", 
             u'standardShipRate': 4.97, 
             u'categoryPath': u'Video Games/Video ' 
                 u'Games', 
             u'itemId': 26096326, 
             u'name': u'The Evil Within (Xbox One)', 
             u'upc': u'093155118539', 
             u'modelNumber': u'093155118539', 
             u'salePrice': 49.96, 
             u'availableOnline': True, 
             u'msrp': 59.99}], u'facets': [], 
    u'start': 1, u'numItems': 1, u'query': u'093155118539', 
    u'totalResults': 1, u'responseGroup': u'base'} 

您在u'items'看到的是項目的列表對象。 items的每個元素是一個項目的字典對象。 - >在u'items'的每個元素中,您會發現salePrice,name,modelNumber

注意:這是modelNumber,不modelnumber

fields_2_get = [u'salePrice',u'name',u'modelNumber'] 
data = response.json() 
for item in data[u'items']: 
    for field_name in fields_2_get: 
     print(field + ": " + str(item.get(field_name, None))) 

request對象不具備的功能find()。所以request.find("xxx")將無法​​正常工作。

1

Python通常在單行上打印出dict s等,這與API無關。如果你想很好地打印出來供自己觀看的嘗試:

from pprint import pprint 
pprint(data) 

或:

print json.dumps(data, indent=4)