输出

时间:2016-04-21 14:14:45

标签: python json

我是json解析的新手,但我想从这个json响应中获得salePrice,name,modelnumber。响应似乎全部都在一线,我没有运气获得数据。

`{u'sort':u'relevance',u'items':[{u'marketplace':False,u'imageEntities':[{u'entityType':u'SECONDARY',u'thumbnailImage' :你'http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg',u'largeImage':你'http://i5.walmartimages.com/dfw/dce07b8c-3397/k2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg',u'mediumImage':你'http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363-b442-468e-8601-88273996cd30.v1.jpg'},{u'entityType':u'SECONDARY', u'thumbnailImage':你'http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2-_e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg',u'largeImage':你'http://i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg',u'mediumImage':你'http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg'},{u'entityType':你'SECONDARY',u'thumbnailImage':你'http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg',u'largeImage':你'http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg',u'mediumImage':你'http://i5.walmartimages.com/dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg'},{u' entityType':u'SECONDARY',u'thumbnailImage':你'http://i5.walmartimages.com/dfw/dce07b8c-1ba6/k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1.jpg',u'largeImage':你'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg',u'mediumImage':你'http://i5.walmartimages.com/dfw/dce07b8c-5d76/k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg'追溯(最近的电话最后一次): },{u'entityType':u'SECONDARY',u'thumbnailImage':你'http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg',u'largeImage':你'http://i5.walmartimages.com/dfw/dce07b8c-a419/k2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg',u'mediumImage':你'{{ 3}}'},{u'entityType':u'PRIMARY',u'thumbnailImage':你'http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98-52e5-4461-8587-0007f84dee24.v1.jpg',u'largeImage':你'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',u'mediumImage': u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg'}],u'numReviews':4,u'productUrl':你'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg',u'customerRatingImage':你'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'addToCartUrl' :u'http://i2.walmartimages.com/i/CustRating/4_8.gif',u'shortDescription':u“由Shinji Mikami开发 - 创造性的生化危机系列的创造者 - 以及Tango Gameworks的才华横溢的团队,The Evil Within体现了纯粹生存恐怖的意义。一个令人不安的现实,你试图摆脱只能存在于最恐怖的世界中的扭曲阴谋。面对扭曲的生物并体验真正的恐怖,所有这些都是通过定制版本的id Tech 5引擎实现的尖端照明和动画所提升的在调查可怕的大屠杀现场时,侦探塞巴斯蒂安卡斯特拉诺斯和他的伙伴们遇到了一股神秘而强大的力量。其他军官的笑声,塞巴斯蒂安遭到伏击并被打昏。当他醒来时,他发现自己陷入了一个疯狂的世界,那里有可怕的生物在死者中徘徊。面对难以想象的恐怖,为生存而战,塞巴斯蒂安开始了一场可怕的旅程,揭开了这股邪恶力量背后的东西。“,'u'customerRating':u'4.75',u'affiliateAddToCartUrl':你'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',你'mediumImage':你'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'productTrackingUrl':你'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg',u'stock':u'Available',u'categoryNode':u'2636_1040579',u'giftOptions ':{u'allowGiftWrap':False,u'allowGiftMessage':False,u'allowGiftReceipt':False},u'largeImage':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'parentItemId':26096326,u'thumbnailImage' :u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',u'bundle':False,u'longDescription':u“< ul>< li> Pure Survival Horror Returns - 生存恐怖的父亲Shinji Mikami回归直接令人不寒而栗的新游戏,令人难以忘怀的叙事。当你探索游戏的折磨世界时,紧张和焦虑会急剧增加。当你为生存而战时,你将拥有有限的资源,并在恐怖和行动的完美结合中经历深刻的恐惧。< / li>< li>残酷的陷阱和扭曲的生物 - 面对无法想象的恐怖和残忍的陷阱你很难在压倒性的赔率下生存。通过对抗势不可挡的生物使用相同的恶魔装置来对抗自己。< / li>< li>在一个不确定的世界中的未知威胁 - 在一个扭曲和扭曲你的世界中,神秘和邪恶的恐惧正在逼近。走廊,墙壁,门和整个建筑物实时变化,陷入困境的玩家在任何时候和任何方向都可能出现威胁。< / li>< li>恐怖的新面孔 - 高度制作环境,可怕的焦虑和错综复杂的故事编织在一起,创造一个沉浸式的世界,将玩家带到紧张的高度。通过发现邪恶的内心,击败难以逾越的恐怖并体验极致的刺激。< / li>< li> ESRB评级:血与戈尔的成熟,强烈的语言和强烈的暴力< / li>< / ul>“,u 'standardShipRate':4.97,u'categoryPath':u'Video Games / Video 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'}

这是我的代码。

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 doc - 我没有提供丢弃的API密钥。 http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg

2 个答案:

答案 0 :(得分:1)

Python通常在一行上打印出dict等,这与API无关。如果您想为自己的观看打印得很好,请尝试:

from pprint import pprint
pprint(data)

或:

print json.dumps(data, indent=4)

答案 1 :(得分:1)

您需要知道解析它的响应格式。我看到你使用了requests.get()

收到请求后

request = requests.get(...)

request.json()将返回JSON响应的dict对象。它看起来像(格式化后,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"<ul><li>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.</li><li>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.</li><li>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.</li><li>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.</li><li>ESRB Rating: MATURE with Blood and Gore, Strong Language, and Intense Violence</li></ul>",
                                        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的每个元素都是项目的dict对象。 - >在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")无效。