【问题标题】:While trying to print the json data in jupyter notebook, I am getting JSON decode error?尝试在 jupyter notebook 中打印 json 数据时,出现 JSON 解码错误?
【发布时间】:2022-01-10 21:02:26
【问题描述】:

我正在尝试在 jupyter notebook 中打印 JSON 数据,但我得到了jsondecodeerror

代码

import json
people_string ='''
{
"people":[
"name":"John Smith",
"phone":"615-555-7164",
"emails":["johnsmith@boguseemail.com","john.smith@work-place.com"],
"has_license":false
},
{
"name":"Jane Doe",
"phone":"560-555-5153",
"emails":null,
"has_license":true

}
]  
}

data=json.loads(people_string)
print(data)

【问题讨论】:

    标签: python json


    【解决方案1】:

    人的第一个元素中缺少开头{

    { 
       "people":[
          {
              "name":"John Smith",
              "phone":"615-555-7164",
              "emails": [
                  "johnsmith@boguseemail.com",
                  "john.smith@work-place.com"
              ], 
              "has_license":false
          }, { 
              "name":"Jane Doe",
              "phone":"560-555-5153", 
              "emails":null,
              "has_license":true
           }
       ]  
    }
    

    【讨论】:

      【解决方案2】:

      您的 Json 中有语法错误,这是格式正确的版本:

      import json
      
      people_string = '''
      {
          "people": [
              {
                  "name": "John Smith",
                  "phone": "615-555-7164",
                  "emails": [
                      "johnsmith@boguseemail.com",
                      "john.smith@work-place.com"
                  ],
                  "has_license": false
              },
              {
                  "name": "Jane Doe",
                  "phone": "560-555-5153",
                  "emails": null,
                  "has_license": true
              }
          ]
      }
      '''
      
      data = json.loads(people_string)
      print(data)
      

      【讨论】:

        【解决方案3】:

        出于调试目的,您可以使用 JSON 查看器(例如 this one)来验证您的 JSON 并更好地对其进行可视化。

        粘贴上面的 JSON,我遇到了以下突出显示的问题:

        Parse error on line 3:
        {"people":["name":"John Smith""phone
        -----------------^
        Expecting 'EOF', '}', ',', ']', got ':'
        

        修复似乎很简单:您可以在[ 的列表初始开始之后添加一个{ - 左大括号以表示地图对象的开始。一旦我这样做了,JSON 似乎是有效的,并且能够按预期加载和可视化数据。

        【讨论】:

          猜你喜欢
          • 2017-11-25
          • 2020-02-08
          • 2020-10-15
          • 2019-06-11
          • 2016-04-20
          • 2020-06-08
          • 2022-01-04
          • 2022-09-22
          • 1970-01-01
          相关资源
          最近更新 更多