您的位置:首页 > 编程语言 > Python开发

[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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: