Solve the problem of json.decoder.JSONDecodeError: Expecting value: in Python

When learning Python language using json library to parse network data, I encountered two compilation errors: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:and json.decoder.JSONDecodeError: Expecting value:. It took some time to find the reason. I will record and summarize here, hoping to be helpful to students who learn Python.

The initial program I run is as follows:

import json

data='''
{
'name' : 'A',
'phone': { 'type' : 'intl', 'number' : +1 23456 },
'email' : {'hide' : 'yes'}
}'''

info=json.loads(data)
print("Name:",info["name"])
print("EmailAttri:",info["email"]["hide"])

After running, an error is reported, and the error is displayed json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:. In the original data format, the string type data should use double quotation marks "instead of single quotation marks'.

After the single quotes in the eleven change overnight, still compiler error: json.decoder.JSONDecodeError: Expecting value:. I thought it was a code format (indentation) problem. After repeated modifications, I still reported an error. This puzzled me. I searched the Internet for a solution to this error, but I didn't find a suitable answer. Finally, I carefully compared the source code of the teacher one by one, and found that the problem actually lies in "number"this element. I used its value as a number. In fact, it +1 23456is a string type here , so double quotes are needed. The modified program is as follows and runs correctly.

import json

data='''
{
"name" : "A",
"phone": { "type" : "intl", "number" : "+1 23456" },
"email" : {"hide" : "yes"}
}'''

info=json.loads(data)
print("Name:",info["name"])
print("EmailAttri:",info["email"]["hide"])

There is another solution to this problem, which is to +1 23456rewrite it as an 123456int type so that there is no need to add double quotes.

When I searched the problem on the Internet, I found that many people also encountered json.decoder.JSONDecodeError: Expecting value:this error. From the process of solving it, I think the main reason is that the format of the data is incorrect. Therefore, if the data is crawled from the Internet, you need to check whether the data format setting meets the requirements of json, so that the program can be compiled successfully.

Guess you like

Origin blog.csdn.net/applebear1123/article/details/103432918