代码之家  ›  专栏  ›  技术社区  ›  Vasanth Raghavan

以下是有效的JSON吗?我怎么用python把它转换成dict呢

  •  1
  • Vasanth Raghavan  · 技术社区  · 7 年前

    以下是有效的JSON吗?

     "AGENT ": {
        "pending": [],
        "active": null,
        "completed": [{}]
     },
     "MONITORING": {
        "pending": [],
        "active": null,
        "completed": [{}]
     }
    

    JSON验证程序站点( https://jsonlint.com/ )说它不是。我怎么能使它成为一个有效的JSON?将其转换为python中的dict将截断JSON块(“agent”部分)。如何在不丢失JSON块的情况下将此块转换为python中的dict?这是从GET请求返回的JSON。使用下列方法不起作用

    response = requests.get(<url>)
    data = response.content
    json_data = json.dumps(data)
    item_dict = json.loads(data)
    item_dict = data
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Eugene Primako    7 年前

    {
     "AGENT ": {
        "pending": [],
        "active": null,
        "completed": [{}]
     },
     "MONITORING": {
        "pending": [],
        "active": null,
        "completed": [{}]
     }
    }
    

    In [27]: json.loads('''{
       ....:  "AGENT ": {
       ....:     "pending": [],
       ....:     "active": null,
       ....:     "completed": [{}]
       ....:  },
       ....:  "MONITORING": {
       ....:     "pending": [],
       ....:     "active": null,
       ....:     "completed": [{}]
       ....:  }
       ....: }''')
    Out[27]: 
    {u'AGENT ': {u'active': None, u'completed': [{}], u'pending': []},
     u'MONITORING': {u'active': None, u'completed': [{}], u'pending': []}}
    

    item_dict = requests.get(<url>).json()