'[{"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  \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  \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":""}]'
如何将此str转换为dicts列表?我尝试了一些方法,但都失败了。该字符串表示包含2个大字符串的列表,并且一个字典包含嵌套的小字典。预期的结果是:
[{dict1},{dict2}]
答案 0 :(得分:3)
您有 JSON字符串,因此请使用json
module对此进行解码:
import json
decoded = json.loads(encoded)
decoded
是一个Python列表;然后,您可以在列表中寻址每个字典,或使用解压缩将两个字典分配给两个名称:
dictionary1, dictionary2 = decoded
如果您使用requests
library,则可以使用response.json()
method加载内容:
decoded = response.json()
在这种特殊情况下,您似乎有GBK encoded data(或者GB2312,也就是前任)。
这远远超出了JSON标准(实际上需要使用其中一个UTF编解码器),并且您需要告诉json.loads()
所使用的编解码器:
decoded = json.loads(encoded, 'gbk')
requests
库将使用服务器与响应一起发送的任何编解码器,否则将使用字符集检测技术来尝试找到要使用的正确编解码器。
解码后的结果如下:
>>> 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  \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  \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''}]