【问题标题】:Simple Json decoding with SimpleJSON - Python使用 SimpleJSON 进行简单 Json 解码 - Python
【发布时间】:2011-05-27 12:24:22
【问题描述】:

我刚刚开始学习 python,并且正在尝试使用 google api。但是我在尝试使用 simplejson 解析 JSON 时遇到了障碍。

如何从下面的混乱中提取单个值(即产品或品牌字段)

{'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"},
                        '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$

【问题讨论】:

  • 要获得您可以做的第一件商品的品牌:data['items'][0]['product']['brand']
  • 非常感谢,太好了:)
  • 看起来这是无效的,不会解析,因为一些以$ 结尾的行没有右引号或逗号

标签: json python-3.x simplejson


【解决方案1】:

您在问题中粘贴的 JSON 无效。但是当你解决了这个问题时,这里是使用 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'])

还有一个指向documentation的链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多