如何修复ValueError:预期属性名称:第4行第1列(字符43)

时间:2018-12-30 09:11:26

标签: python json django python-2.7

当我尝试运行python manage.py runserver代码时出现错误。它的回溯很奇怪,

我尝试了

  

JSON ValueError: Expecting property name: line 1 column 2 (char 1)

和所有类似的问题,但没有得到我真正要面对的问题。

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/tousif/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/tousif/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 308, in execute
    settings.INSTALLED_APPS
  File "/home/tousif/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/tousif/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/tousif/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/tousif/Desktop/ITP/ITP/itpcrm/itpcrm/settings.py", line 55, in <module>
    cfg = json.loads(open('/home/tousif/Desktop/ITP/ITP/itpcrm/config.json', 'r').read())
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 4 column 1 (char 43)

我的config.json文件中包含凭据等(我将凭据更改为在此处发布)。然后从实时服务器上获取了此文件,该文件可以正常工作,但是在本地却给出了此错误。

{
        "dev": {
                "db": {
​
                        "ENGINE": "django.db.backends.mysql",
                        "NAME": "itpcrm",
                        "USER": "root",
                        "PASSWORD": "password",
                        "HOST": "localhost",
                        "PORT": "3306"
                },
                "jwt_key": "GRESDFwef3452fwefer",
        "voice_api_url": "http://192.112.255.32:9040",
                "voice_api_key": "3123",
        "auth_api_key": "379h4f73f",
        "provisioner_api_key": "abc",
        "quote_approval_url": "http://192.112.255.145:9998/quotes/customer-approval?token=",
        "docusign_base_url": "https://demo.docusign.net/restapi",
        "docusign_integrator_key": "8a256bde-405b",
        "docusign_oauth_base_url": "account-d.docusign.com",
        "docusign_redirect_uri": "http://192.112.255.145:9998/api/callbacks/docusign",
        "docusign_private_key_filename": "/home/itp/docusign-examples/keys/docusign_private_key.txt",
        "docusign_user_id": "7f2444f-ae99-54922fec68f6",
        "docusign_user_name": "dor.com"
        },
        "prod": {
                "db": {
​
                        "ENGINE": "django.db.backends.mysql",
                        "NAME": "itp",
                        "USER": "it",
                        "PASSWORD": "password",
                        "HOST": "192.168.3.111",
                        "PORT": "3306"
                },
                "jwt_key": "rRregrgERg54g564heRGRfdsger",
        "voice_api_url": "https://api.crm.itpscorp.com/itpvoice",
                "voice_api_key": "abc1",
        "auth_api_key": "379h4f73f3279fy927yf928oowqofabdbf",
        "provisioner_api_key": "abc123123",
        "quote_approval_url": "http://192.112.255.145:9998/quotes/customer-approval?token=",
        "docusign_base_url": "https://demo.docusign.net/restapi",
        "docusign_integrator_key": "8a256bde-405b-4032-bf24-be0245631f03",
        "docusign_oauth_base_url": "account-d.docusign.com",
        "docusign_redirect_uri": "http://192.112.255.145:9998/api/callbacks/docusign",
        "docusign_private_key_filename": "/home/itp/docusign-examples/keys/docusign_private_key.txt",
        "docusign_user_id": "7f26f6bb-8a39-444f-ae99-54922fec68f6",
        "docusign_user_name": "docusign@itpfiber.com"
        },
        "mode": "dev"
}

1 个答案:

答案 0 :(得分:2)

“ db”之后的空行以Unicode代码点0x200B(“零宽度空间”)开头。这就是让JSON解码器跳闸的原因。

我将文本复制到gvim并进行了截图。见下文。

space in JSON

删除这些字符(或整行),即可正常工作...

(使用十六进制编辑器查看JSON文件也可以清楚地显示问题。)

如果仔细查看错误消息,则可以正确识别问题:

  

ValueError:期望的属性名称:第4行第1列(字符43)

这个故事的寓意:寻找空白代码点。