来源:python中国网 时间:2019-10-24

  用json.loads()后报错如下:json.decoder.JSONDecodeError: Invalid control character at: line 1 column 517

  str_json = 'xxxxxxxxxxxxxxxxxxx'

  dic = json.loads(str_json)

  报错:

  son.decoder.JSONDecodeError: Invalid control character at: line 1 column 517

  原因(谷歌一把找到的):

  The problem is your unicode string contains carriage returns (\r) and newlines (\n) within a string literal in the JSON data. If they were meant to be part of the string itself, they should be escaped appropriately. If they weren't meant to be part of the string, they shouldn't be in your JSON either.

  If you can't fix where you got this JSON string to produce valid JSON, you could either remove the offending characters

  大概意思是:

  字符串含有换行\n、\r\r之类的字符,如果这些不是你需要的字符串数据,要干掉!

  然后做替换:

  str_json = str_json.replace('\n','').replace('\r\n','')

  结果还是报错

  son.decoder.JSONDecodeError: Invalid control character at: line 1 column 517

  然后加参数(百度一把找到的)

  dic = json.loads(str_json,strict=False)

  运行OK!

  注意:

  strict=False只加上这个参数不去除换行也是报错!