Search code examples
pythonstringlistdictionarytypeconverter

How to convert a JSON string into a Python data structure


'[{"append":null,"appendCanExplainable":false,"appendList":[],"auction":{"aucNumId":"35179051643","auctionPic":"http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg","link":"http://item.taobao.com/item.htm?id=35179051643","sku":"\xd1\xab\xb7\xd6\xc0\xe0:\xc9\xab\xbb\xd2\xcf\xdf\xbd\xf4\xc9\xed\xb3\xa4\xbf\xe3 &nbsp\xb3\xdf\xc2\xeb:M-170M-55-62KG","thumbnail":"","title":"\xcb\xb9\xbd\xf4\xc9\xed\xbf\xe3 \xb5\xaf\xc1\xa6\xd7\xe3\xc7\xf2\xd4\xaf\xbd\xa1\xc9\xed\xbf\xe3 PRO \xc4\xd0 \xb4\xf2\xb5\xd7\xd1\xb5\xc1\xb7\xb3\xa4\xbf\xe3\xcb\xd9\xb8\xc9"},"award":"","bidPriceMoney":{"amount":35,"cent":3500,"centFactor":100,"currency":{"currencyCode":"CNY","defaultFractionDigits":2,"symbol":"\xa3\xa4"},"currencyCode":"CNY","displayUnit":"\xd4\xaa"},"buyAmount":1,"content":"\xba\xc3\xc6\xc0\xa3\xa1","creditFraudRule":0,"date":"2014\xc4\xea12\xd4\xc220\xc8\xd5 15:41","dayAfterConfirm":0,"enableSNS":false,"from":"","lastModifyFrom":0,"payTime":{"date":18,"day":4,"hours":13,"minutes":4,"month":11,"seconds":37,"time":1418879077000,"timezoneOffset":-480,"year":114},"photos":[],"promotionType":"\xbb\xee\xb6\xaf\xb4\xd9\xcf\xfa  ","propertiesAvg":"0.0","rate":"1","rateId":231421178840,"raterType":0,"reply":null,"shareInfo":{"lastReplyTime":"","pic":0,"reply":0,"share":false,"userNumIdBase64":""},"showCuIcon":true,"showDepositIcon":false,"spuRatting":[],"status":0,"tag":"","useful":0,"user":{"anony":true,"avatar":"http://a.tbcdn.cn/app/sns/img/default/avatar-40.png","displayRatePic":"b_red_3.gif","nick":"y***6","nickUrl":"","rank":65,"rankUrl":"","userId":"","vip":"","vipLevel":0},"validscore":1,"vicious":""},{"append":null,"appendCanExplainable":false,"appendList":[],"auction":{"aucNumId":"35179051643","auctionPic":"http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg","link":"http://item.taobao.com/item.htm?id=35179051643","sku":"\xd1\xd5\xc9\xab\xb7\xd6\xc0\xe0:\xba\xda\xc9\xab\xba\xda\xcf\xdf\xbd\xf4\xc9\xed\xb3\xa4\xbf\xe3 &nbsp\xb3\xdf\xc2\xeb:S-160m-45~55KG","thumbnail":"","title":"\xc7\xf2\xc9\xed\xbf\xe3\xb4\xf2\xb5\xd7\xd1\xb5\xc1\xb7\xb3\xa4\xbf\xe3\xcb\xd9\xb8\xc9"},"award":"","bidPriceMoney":{"amount":35,"cent":3500,"centFactor":100,"currency":{"currencyCode":"CNY","defaultFractionDigits":2,"symbol":"\xa3\xa4"},"currencyCode":"CNY","displayUnit":"\xd4\xaa"},"buyAmount":1,"content":"\xba\xc3\xc6\xc0\xa3\xa1","creditFraudRule":0,"date":"2014\xc4\xea12\xd4\xc220\xc8\xd5 15:37","dayAfterConfirm":0,"enableSNS":false,"from":"","lastModifyFrom":0,"payTime":{"date":17,"day":3,"hours":17,"minutes":43,"month":11,"seconds":47,"time":1418809427000,"timezoneOffset":-480,"year":114},"photos":[],"promotionType":"\xbb\xee\xb6\xaf\xb4\xd9\xcf\xfa  ","propertiesAvg":"0.0","rate":"1","rateId":231441191365,"raterType":0,"reply":null,"shareInfo":{"lastReplyTime":"","pic":0,"reply":0,"share":false,"userNumIdBase64":""},"showCuIcon":true,"showDepositIcon":false,"spuRatting":[],"status":0,"tag":"","useful":0,"user":{"anony":true,"avatar":"http://a.tbcdn.cn/app/sns/img/default/avatar-40.png","displayRatePic":"b_blue_3.gif","nick":"\xc2\xb7***0","nickUrl":"","rank":1235,"rankUrl":"","userId":"","vip":"","vipLevel":0},"validscore":1,"vicious":""}]'

How can I convert this str to list of dicts ? I have tried some methods, but failed. The string represents a list containing 2 big dicts, and one dict contains nested small dicts. The expected result is:

[{dict1},{dict2}]


Solution

  • You have a JSON string, so use the json module to decode this:

    import json
    
    decoded = json.loads(encoded)
    

    decoded is then a Python list; you can then address each dictionary in a list, or use unpacking to assign two dictionaries to two names:

    dictionary1, dictionary2 = decoded
    

    If you are using the requests library then you can use the response.json() method to load the content:

    decoded = response.json()
    

    In this specific case you appear to have GBK encoded data however (or perhaps GB2312, a predecessor).

    This goes well outside the JSON standard (which actually requires one of the UTF codecs to be used), and you'll need to tell json.loads() about the codec used:

    decoded = json.loads(encoded, 'gbk')
    

    The requests library will use whatever codec the server sent along with the response, or will otherwise use a characterset detection technique to try and find the right codec to use.

    The result, when decoded, then looks like:

    >>> decoded = json.loads(encoded, 'gbk')
    >>> pprint(decoded)
    [{u'append': None,
      u'appendCanExplainable': False,
      u'appendList': [],
      u'auction': {u'aucNumId': u'35179051643',
                   u'auctionPic': u'http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg',
                   u'link': u'http://item.taobao.com/item.htm?id=35179051643',
                   u'sku': u'\u52cb\u5206\u7c7b:\u8272\u7070\u7ebf\u7d27\u8eab\u957f\u88e4 &nbsp\u5c3a\u7801:M-170M-55-62KG',
                   u'thumbnail': u'',
                   u'title': u'\u65af\u7d27\u8eab\u88e4 \u5f39\u529b\u8db3\u7403\u8f95\u5065\u8eab\u88e4 PRO \u7537 \u6253\u5e95\u8bad\u7ec3\u957f\u88e4\u901f\u5e72'},
      u'award': u'',
      u'bidPriceMoney': {u'amount': 35,
                         u'cent': 3500,
                         u'centFactor': 100,
                         u'currency': {u'currencyCode': u'CNY',
                                       u'defaultFractionDigits': 2,
                                       u'symbol': u'\uffe5'},
                         u'currencyCode': u'CNY',
                         u'displayUnit': u'\u5143'},
      u'buyAmount': 1,
      u'content': u'\u597d\u8bc4\uff01',
      u'creditFraudRule': 0,
      u'date': u'2014\u5e7412\u670820\u65e5 15:41',
      u'dayAfterConfirm': 0,
      u'enableSNS': False,
      u'from': u'',
      u'lastModifyFrom': 0,
      u'payTime': {u'date': 18,
                   u'day': 4,
                   u'hours': 13,
                   u'minutes': 4,
                   u'month': 11,
                   u'seconds': 37,
                   u'time': 1418879077000,
                   u'timezoneOffset': -480,
                   u'year': 114},
      u'photos': [],
      u'promotionType': u'\u6d3b\u52a8\u4fc3\u9500  ',
      u'propertiesAvg': u'0.0',
      u'rate': u'1',
      u'rateId': 231421178840,
      u'raterType': 0,
      u'reply': None,
      u'shareInfo': {u'lastReplyTime': u'',
                     u'pic': 0,
                     u'reply': 0,
                     u'share': False,
                     u'userNumIdBase64': u''},
      u'showCuIcon': True,
      u'showDepositIcon': False,
      u'spuRatting': [],
      u'status': 0,
      u'tag': u'',
      u'useful': 0,
      u'user': {u'anony': True,
                u'avatar': u'http://a.tbcdn.cn/app/sns/img/default/avatar-40.png',
                u'displayRatePic': u'b_red_3.gif',
                u'nick': u'y***6',
                u'nickUrl': u'',
                u'rank': 65,
                u'rankUrl': u'',
                u'userId': u'',
                u'vip': u'',
                u'vipLevel': 0},
      u'validscore': 1,
      u'vicious': u''},
     {u'append': None,
      u'appendCanExplainable': False,
      u'appendList': [],
      u'auction': {u'aucNumId': u'35179051643',
                   u'auctionPic': u'http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg',
                   u'link': u'http://item.taobao.com/item.htm?id=35179051643',
                   u'sku': u'\u989c\u8272\u5206\u7c7b:\u9ed1\u8272\u9ed1\u7ebf\u7d27\u8eab\u957f\u88e4 &nbsp\u5c3a\u7801:S-160m-45~55KG',
                   u'thumbnail': u'',
                   u'title': u'\u7403\u8eab\u88e4\u6253\u5e95\u8bad\u7ec3\u957f\u88e4\u901f\u5e72'},
      u'award': u'',
      u'bidPriceMoney': {u'amount': 35,
                         u'cent': 3500,
                         u'centFactor': 100,
                         u'currency': {u'currencyCode': u'CNY',
                                       u'defaultFractionDigits': 2,
                                       u'symbol': u'\uffe5'},
                         u'currencyCode': u'CNY',
                         u'displayUnit': u'\u5143'},
      u'buyAmount': 1,
      u'content': u'\u597d\u8bc4\uff01',
      u'creditFraudRule': 0,
      u'date': u'2014\u5e7412\u670820\u65e5 15:37',
      u'dayAfterConfirm': 0,
      u'enableSNS': False,
      u'from': u'',
      u'lastModifyFrom': 0,
      u'payTime': {u'date': 17,
                   u'day': 3,
                   u'hours': 17,
                   u'minutes': 43,
                   u'month': 11,
                   u'seconds': 47,
                   u'time': 1418809427000,
                   u'timezoneOffset': -480,
                   u'year': 114},
      u'photos': [],
      u'promotionType': u'\u6d3b\u52a8\u4fc3\u9500  ',
      u'propertiesAvg': u'0.0',
      u'rate': u'1',
      u'rateId': 231441191365,
      u'raterType': 0,
      u'reply': None,
      u'shareInfo': {u'lastReplyTime': u'',
                     u'pic': 0,
                     u'reply': 0,
                     u'share': False,
                     u'userNumIdBase64': u''},
      u'showCuIcon': True,
      u'showDepositIcon': False,
      u'spuRatting': [],
      u'status': 0,
      u'tag': u'',
      u'useful': 0,
      u'user': {u'anony': True,
                u'avatar': u'http://a.tbcdn.cn/app/sns/img/default/avatar-40.png',
                u'displayRatePic': u'b_blue_3.gif',
                u'nick': u'\u8def***0',
                u'nickUrl': u'',
                u'rank': 1235,
                u'rankUrl': u'',
                u'userId': u'',
                u'vip': u'',
                u'vipLevel': 0},
      u'validscore': 1,
      u'vicious': u''}]