将Json字符串转换为Python字典时出错

问题描述 投票:0回答:1

我有如下的json文件,并尝试转换为python字典,但出现错误

{
  "response": {
    "dev_log": {
      "data": [
        {
          "id": "1",
          "timestamp": "2020-01-16 10:11:12",
          "email": "[email protected]"
        },
        {
          "id": "2",
          "timestamp": "2020-02-27 15:33:34",
          "email": "[email protected]"
        },
        {
          "id": "3",
          "timestamp": "2020-02-27 15:34:07",
          "email": "[email protected]"
        }
      ],
      "total_dev_log": "1423"
    },
    "client_log": {
      "data": [
        {
          "customer_city": "LONDON",
          "customer_login": "AAAAAAAAAAAAAA",
          "customer_state": "MC",
          "details": "aaaaaaaaaaa-bbbbbbbbbbbbbbb-cccccccccccccc ",
          "log_number": "1",
          "dept": "Sales",
          "staff_id": "S123",
          "staff_name": "EricY",
          "timestamp": "2020-02-27 15:57:24"
        },
        {
          "customer_city": "SINGAPORE",
          "customer_login": "BBBBBBBBBBBBB",
          "customer_state": "XX",
          "details": "ddddddddd-eeeeeeeeeeee-ffffffffffff ",
          "log_number": "1",
          "dept": "Eng",
          "staff_id": "S456",
          "staff_name": "YongG",
          "timestamp": "2020-02-27 15:57:24"
        }
      ],
      "total_hero_query": "13"
    },
    "response_time": "0.723494",
    "transaction_id": "909122",
    "transaction_status": "OK",
    "transaction_time": "Fri Feb 28 15:27:51 2020"
  }
}

我能够通过http://jsonviewer.stack.hu查看为有效的json。我相信它是有效的json字符串格式。

Normallay我只是使用下面的代码来读取json文件并将其转换为dict,但出现错误。

with open('datfile.json', 'r') as f:
   datDict = json.load(f)

错误

Traceback (most recent call last):
  File "strg2dict.py", line 4, in <module>
    json_dict = json.load(JSON)
  File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
  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 22 column 5 (char 466)

我有其他人的解决方案,但仍然无法找到解决方案。请进一步告知。谢谢

@@@@@@@@@@@@@@@@@@@@@@@@@@@@有逗号“ total_dev_log”:“ 1423”,

===>删除逗号“ total_dev_log”:“ 1423”

已解决>谢谢

python json list dictionary
1个回答
0
投票

您的json文件在第21行包含尾随逗号:

"total_dev_log": "1423",
                       ^ 

JSON规范does not allow for trailing commas。只需删除此逗号即可更正错误。

© www.soinside.com 2019 - 2024. All rights reserved.