Home  >  Q&A  >  body text

python - pyhon,json.loads()出来的数据为unicode,如何转化为dict。

目前是自己在爬取一个网页,POST请求后发回一段数据。

response = urllib2.urlopen(request)      
page = response.read()
datas = json.dumps(page, ensure_ascii=False)
jsondatas = json.loads(datas)

此处jsondatas类型为unicode,但我私下试了下,类型却是dict。

然后去看了下文档定义。

str类型会转化unicode,但为什么自己尝试却是dict。
另外该如何将jsondatas转化为dict类型?我需要提取其中的数据。

PHPzPHPz2741 days ago849

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 15:31:19

    jsondatas is a dict type. What the document means is that strings in json will be converted to unicode, and objects in json will be converted to dict, such as:

    {
        "key": "value",
        "str": "这个字符串会被转化成unicode", 
        "obj": {
            "objkey", "objvalue"
        }
    }
    

    The value string of "str" ​​in this json will be converted into unicode, and the value of "obj" will be converted into an object

    reply
    0
  • Cancelreply