[Python]json对象转换出错expected|[Python]json对象转换出错expected string or buffer python

【问题】 今天在使用python中的json转换碰到一个问题:

[Python]json对象转换出错expected|[Python]json对象转换出错expected string or buffer python
文章图片


【代码】 comments.json

{ "count":"2", "page":"1", "comments":[ { "content":"helloworld", "user":{ "id":"0001", "name":"xiaosi" }, "source":{ "link":"http://mobile.youku.co", "name":"iPhone" } }, { "content":"welcome to china", "user":{ "id":"0002", "name":"sjf" }, "source":{ "link":"http://mobile.youku.co", "name":"android" } } ] }

Test.py
# coding=utf-8import json file = file("D:\\项目\python\comments.json") data = https://www.it610.com/article/json.loads(file)



【分析解决】 经过调试,最终发现,python中默认使用单引号表示字符串"'" 所以当,使用字符串符值以后,python会把双引号转换为单引号。

举例:

s = { "count":"2", "page":"1", "comments":[ { "content":"helloworld", "user":{ "id":"0001", "name":"xiaosi" }, "source":{ "link":"http://mobile.youku.co", "name":"iPhone" } }, { "content":"welcome to china", "user":{ "id":"0002", "name":"sjf" }, "source":{ "link":"http://mobile.youku.co", "name":"android" } } ] }print s


[Python]json对象转换出错expected|[Python]json对象转换出错expected string or buffer python
文章图片




而json是不支持单引号的。可以用下面的方法转换
json_string=json.dumps(s)
str=json.loads(json_string)











【[Python]json对象转换出错expected|[Python]json对象转换出错expected string or buffer python】

    推荐阅读