简体   繁体   English

如何将 JSON 字符串转换为 Python 数据结构

[英]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 ?如何将此 str 转换为 dicts 列表? I have tried some methods, but failed.我尝试了一些方法,但都失败了。 The string represents a list containing 2 big dicts, and one dict contains nested small dicts.该字符串表示一个包含 2 个大字典的列表,一个 dict 包含嵌套的小字典。 The expected result is:预期的结果是:

[{dict1},{dict2}]

You have a JSON string , so use the json module to decode this:你有一个JSON string ,所以使用json模块来解码:

import json

decoded = json.loads(encoded)

decoded is then a Python list; decoded是一个 Python 列表; 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:如果您使用的是requests库,那么您可以使用response.json()方法来加载内容:

decoded = response.json()

In this specific case you appear to have GBK encoded data however (or perhaps GB2312, a predecessor).在这种特定情况下,您似乎拥有GBK 编码的数据(或者可能是 GB2312,前身)。

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:这完全超出了 JSON 标准(实际上需要使用一种 UTF 编解码器),您需要告诉json.loads()使用的编解码器:

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. requests库将使用服务器随响应发送的任何编解码器,或者将使用字符集检测技术来尝试找到要使用的正确编解码器。

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''}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM