njutwangjian 发表于 2017-11-28 17:43

python json转dict遇ValueError: Expecting property name问题

代码:
        with open(yamlOutPath) as fr:
                content = ''
                for line in fr.readlines():
                        content = content + line
               
                reRes = r'(ok):\s+\[({0,2}\.{0,2}\.{0,2}\.{0,2})\]\s+=\>\s+({.*?})'
                resList = re.findall(reRes, str(content), flags = re.S + re.M)
                resListLen = len(resList)
               
                if resListLen > 0:
                        for iRes in range(resListLen):
                                print('status: %s' %resList)
                                print('ip: %s' %resList)
                                print('msg: %s' %resList)
                               
                                msg = resList.replace('\r\n', '')
                                msg = re.sub(r'\s+"', '"', msg)
                                print('msg type: %s' %type(msg))
                                print('msg pro: %s' %msg)
                                print(json.loads(msg))

                else:
                        print('No content match')
执行结果:
status: ok
ip: 165.254.88.23
msg: {
    "msg": [
      "The mmr started time",
      "The mmr running time is 2 days, 8 hours, 6 minutes, 44 seconds",
      " mmr /etc/cron.d/ssbguard be abled",
      " mmr /etc/cron.d/restartservice be abled",
      "The mmr version is Cloud MMR Release 3.6.109526.1009",
      " mmr suspend",
      " mmr running and number of mmr process is 1",
      " mmr establish connections with port 8802",
      " mmr listening port 443",
      " mmr listening port 8801",
      "The mmr current user count is 0"
    ]
}
msg type: <type 'str'>
msg pro: {"msg": ["The mmr started time", "The mmr running time is 2 days, 8 hours, 6 minutes, 44 seconds", " mmr /etc/cron.d/ssbguard be abled", " mmr /etc/cron.d/restartservice be abled", "The mmr version is Cloud MMR Release 3.6.109526.1009", " mmr suspend", " mmr running and number of mmr process is 1", " mmr establish connections with port 8802", " mmr listening port 443", " mmr listening port 8801", "The mmr current user count is 0"    ]}
Traceback (most recent call last):
File "./call_yaml.py", line 89, in <module>
    call_yaml(yamlPath, inventoryPath, hosts)
File "./call_yaml.py", line 66, in call_yaml
    print(json.loads(msg))
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

我的json字符串格式应该是没问题的,为什么json.loads()会报上述错误呢,麻烦大家帮帮我,谢谢大家

njutwangjian 发表于 2017-11-29 17:55

找到问题所在了,因为字符串中存在控制字符,所以一直报ValueError: Expecting property name: line 1 column 2 (char 1),终究json字符串格式存在问题,删除字符串中的控制字符就可以了
页: [1]
查看完整版本: python json转dict遇ValueError: Expecting property name问题