原文地址:http://www.crifan.com/python_json_loads_valueerror_expecting_property_name_line_1_column_1_char_1/
【问题】
处理:
http://www.yupoo.com/photos/shanshu/87329678/
中的html代码过程中,用代码:
1
|
photoInfoDict
=
json.loads(photoInfoJson);
|
去解码json字符串:
1
|
{id:
'379879-87329678'
,owner:
'379879'
,ownername:
'shanshu'
,title:
'IMG_3464'
,description:
''
,bucket:
'shanshu'
,key:
'CsFzMuHz'
,license:
0
,stats_notes:
0
,albums: [
'379879-181880'
,],tags:[{name:
'20121202'
, author:
'379879'
},{name:
'天平山赏红枫'
, author:
'379879'
}],owner:{id:
379879
,username:
'shanshu'
,nickname:
'shanshu'
}}
|
注:
格式化后,为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
{
id :
'379879-87329678'
,
owner :
'379879'
,
ownername :
'shanshu'
,
title :
'IMG_3464'
,
description :
''
,
bucket :
'shanshu'
,
key :
'CsFzMuHz'
,
license :
0
,
stats_notes :
0
,
albums : [
'379879-181880'
, ],
tags : [{
name :
'20121202'
,
author :
'379879'
}, {
name :
'天平山赏红枫'
,
author :
'379879'
}
],
owner : {
id :
379879
,
username :
'shanshu'
,
nickname :
'shanshu'
}
}
|
结果出错:
photoInfoDict = json.loads(photoInfoJson); return _default_decoder.decode(s) File "D:\tmp\dev_install_root\Python27_x64\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "D:\tmp\dev_install_root\Python27_x64\lib\json\decoder.py", line 382, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 1 (char 1) |
【解决过程】
1.参考:
Error msg from using wrong quotes in JSON is unhelpful
中,解释说是,JSON字符串中,不能包含单引号,而必须是双引号。
2.所以去改为双引号:
1
2
3
4
|
photoInfoJsonDoubleQuote
=
photoInfoJson.replace(
"'"
, '"');
logging.info(
"photoInfoJsonDoubleQuote=%s"
, photoInfoJsonDoubleQuote);
photoInfoDict
=
json.loads(photoInfoJsonDoubleQuote);
logging.info(
"photoInfoDict=%s"
, photoInfoDict);
|
结果错误依旧。
3.看起来,应该是,属性,没有添加双引号所导致的,应该是类似于:
"id":"379879-87329678"
估计才可以的。
4.给定utf8参数:
1
2
3
4
5
|
photoInfoJsonDoubleQuote
=
photoInfoJson.replace(
"'"
, '"');
logging.info(
"photoInfoJsonDoubleQuote=%s"
, photoInfoJsonDoubleQuote);
#photoInfoDict = json.loads(photoInfoJsonDoubleQuote);
photoInfoDict
=
json.loads(photoInfoJsonDoubleQuote,
"UTF-8"
);
logging.info(
"photoInfoDict=%s"
, photoInfoDict);
|
也还是不行:
5.网上找了半天,结果找到自己的帖子:
【已解决】Python中用json.loads去解析字符串出错:ValueError: Expecting property name: line 1 column 51 (char 51)
然后手动处理后,结果用如下代码:
1
2
3
4
5
6
7
8
9
10
11
|
photoInfoJsonAddQuote
=
re.sub(r
"(,?)(\w+?)\s*?:"
, r
"\1'\2':"
, photoInfoJson);
logging.info(
"photoInfoJsonAddQuote=%s"
, photoInfoJsonAddQuote);
photoInfoJsonDoubleQuote
=
photoInfoJsonAddQuote.replace(
"'", "\"");
logging.info("photoInfoJsonDoubleQuote=%s", photoInfoJsonDoubleQuote);
#photoInfoJsonDoubleQuote = photoInfoJson.replace("'"
, '"');
#logging.info("photoInfoJsonDoubleQuote=%s", photoInfoJsonDoubleQuote);
#photoInfoDict = json.loads(photoInfoJsonDoubleQuote);
#photoInfoDict = json.loads(photoInfoJsonDoubleQuote, "UTF-8");
photoInfoDict
=
json.loads(photoInfoJsonDoubleQuote);
logging.info(
"photoInfoDict=%s"
, photoInfoDict);
|
上述问题是解决了,但却又出现其他错误:
ValueError: No JSON object could be decoded
6.详细过程参见:
【已解决】Python中用json.loads解码字符串出错:ValueError: No JSON object could be decoded
【总结】
此处的
ValueError: Expecting property name: line 1 column 1 (char 1)
类型的错误,就是由于JSON中,标准语法中,不支持单引号,
属性或者属性值,都必须是双引号括起来的。
所以,可以用类似于:
1
2
|
addedSingleQuoteJsonStr = re.sub(r "(,?)(\w+?)\s*?:" , r "\1'\2':" , orginalJsonStr);
doubleQuotedJsonStr = addedSingleQuoteJsonStr.replace( "'" , "\"" );
|
的代码,去:
就可以了。