[Python]json对象转换出错expected string or buffer python
2015-01-12 19:00
519 查看
【问题】
今天在使用python中的json转换碰到一个问题:【代码】
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-8 import json file = file("D:\\项目\python\comments.json") data = 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
而json是不支持单引号的。可以用下面的方法转换
json_string=json.dumps(s)
str=json.loads(json_string)
相关文章推荐
- Python出现: TypeError: expected string or buffer
- python中json对象转换出错解决方法
- python写入文本报错TypeError: expected a string or other character buffer object
- django 接受post请求json.dumps()的时候会引发TypeError: 'expected string or buffer'错误
- C#中的DataSet、string、DataTable 、对象转换成Json
- python 中json库的 对象转换问题
- 用flexjson转换对象时,出错Error trying to deepSerialize
- string类型转换为json对象保存
- Python 将类的对象转换成 JSON 详解 - [开发]
- json对象转换成string的方法
- 用flexjson转换对象时,出错Error trying to deepSerialize
- C#中的DataSet、string、DataTable 、对象转换成Json
- 将JSON字符串转换为String对象
- 使用JSON.parse将json字符串转换成json对象的时候会出错
- 一个json string和泛型对象转换的经典实例
- QString 与python string 对象转换
- JSON.stringify()将JSON对象转换为字符串通过Ajax传入到后台处理
- JSON.stringify()将JSON对象转换为字符串通过Ajax传入到后台处理
- 【原】使用Json作为Python和C#混合编程时对象转换的中间文件
- 使用JSON.parse将json字符串转换成json对象的时候会出错