开发者

Simple Json decoding with SimpleJSON - Python

Ive just started learning python and Im having a go at using a google api. But I hit a brick wall trying to parse the JSON with simplejson.

How do I go about pulling single values (ie product or brand fields) out of this mess below

{'currentItemCount': 25,  'etag': '"izYJutfqR9tRDg1H4X3fGx1UiCI/hqqZ6pMwV1-CEu5NSqfJO0Ix-gs"', 'id': 'tag:google.com,2010:shopping/products', 'items': [{'id': 'tag:google.com,2010:shopping/products/1196682/8186421160532506003',
            'kind': 'shopping#product',
            'product': {'author': {'accountId': '1196682',
                                   'name': "Dillard's"},
               开发者_如何学Go         'brand': 'Merrell',
                        'condition': 'new',
                        'country': 'US',
                        'creationTime': '2011-03-10T08:11:08.000Z',
                        'description': u'Merrell\'s "Trail Glove" barefoot running shoe lets your feet follow their natural i$
                        'googleId': '8186421160532506003',
                        'gtin': '00797240569847',
                        'images': [{'link': 'http://dimg.dillards.com/is/image/DillardsZoom/03528718_zi_amazon?$product$'}],
                        'inventories': [{'availability': 'inStock',
                                         'channel': 'online',
                                         'currency': 'USD',
                                         'price': 110.0}],
                        'language': 'en',
                        'link': 'http://www.dillards.com/product/Merrell-Mens-Trail-Glove-Barefoot-Running-Shoes_301_-1_301_5$
                        'modificationTime': '2011-05-25T07:42:51.000Z',
                        'title': 'Merrell Men\'s "Trail Glove" Barefoot Running Shoes'},
            'selfLink': 'https://www.googleapis.com/shopping/search/v1/public/products/1196682/gid/8186421160532506003?alt=js$


The JSON you've pasted in the question is not valid. But when you fixed that here's how to use simplejson:

    import simplejson as json
    your_response_body = '["foo", {"bar":["baz", null, 1.0, 2]}]'
    obj = json.loads(your_response_body)
    print(obj[1]['bar'])

And a link to the documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜