简体   繁体   English

尝试在 jupyter 笔记本中打印 json 数据时,出现 JSON 解码错误?

[英]While trying to print the json data in jupyter notebook, I am getting JSON decode error?

I am trying to print JSON data in jupyter notebook, But I am getting the jsondecodeerror .我正在尝试在 jupyter 笔记本中打印 JSON 数据,但我得到了jsondecodeerror

Code代码

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)

There is a missing opening { in the first element of people.在 people 的第一个元素中缺少一个开头{

{ 
   "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
       }
   ]  
}

There are syntax errors in your Json, here is the version correctly formatted:您的 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)

For debugging purposes, you can use a JSON viewer such as this one to validate your JSON and also to visualize it a little better.出于调试目的,您可以使用 JSON 查看器(例如这个查看器)来验证您的 JSON 并更好地对其进行可视化。

Pasting the JSON above I run into the following issue which gets highlighted:粘贴上面的 JSON 我遇到了以下突出显示的问题:

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

The fix seems straightforward enough: you can add a { - open brace to signal the start of a map object - after the initial start of the list at [ .修复似乎很简单:您可以添加一个{ - 左大括号来表示 map object 的开始 - 在列表的初始开始之后[ Once I did this, the JSON seems to be valid and am able to load and visualize the data as expected.一旦我这样做了,JSON 似乎是有效的,并且能够按预期加载和可视化数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM