Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

arrays - JSONDecodeError; Invalid /escape when parsing from Python

After running my object detection model, it outputs a .json file with the results. In order to actually use the results of the model in my python I need to parse the .json file, but nothing I have tried in order to do it works. I tried just to open and then print the results but I got the error:

json.decoder.JSONDecodeError: Invalid escape: line 4 column 41 (char 60)

If you have any idea what I did wrong, the help would be very much appreciated. My code:

with open(r'C:Yolo_v4darknetuilddarknetx64
esult.json') as result:
    data = json.load(result)
    result.close()

print(data)

My .json file

[
{
 "frame_id":1, 
 "filename":"C:Yolo_v4darknetuilddarknetx64f047.png", 
 "objects": [ 
  {"class_id":32, "name":"right", "relative_coordinates":{"center_x":0.831927, "center_y":0.202225, "width":0.418463, "height":0.034752}, "confidence":0.976091}, 
  {"class_id":19, "name":"h", "relative_coordinates":{"center_x":0.014761, "center_y":0.873551, "width":0.041723, "height":0.070544}, "confidence":0.484339}, 
  {"class_id":24, "name":"left", "relative_coordinates":{"center_x":0.285694, "center_y":0.200752, "width":0.619584, "height":0.032149}, "confidence":0.646595}, 
 ] 
}
]

(There are several more detected objects but did not include them)

question from:https://stackoverflow.com/questions/65910282/jsondecodeerror-invalid-escape-when-parsing-from-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hi you need to use double (backslashes), remove the last comma in the objects property and finally you dont need close the file inside the with block

[
{
 "frame_id":1, 
 "filename":"C:\Yolo_v4\darknet\build\darknet\x64\f047.png", 
 "objects": [ 
    {"class_id":32, "name":"right", "relative_coordinates":{"center_x":0.831927,     "center_y":0.202225, "width":0.418463, "height":0.034752}, "confidence":0.976091}, 
    {"class_id":19, "name":"h", "relative_coordinates":{"center_x":0.014761,     "center_y":0.873551, "width":0.041723, "height":0.070544}, "confidence":0.484339}, 
    {"class_id":24, "name":"left", "relative_coordinates":{"center_x":0.285694,     "center_y":0.200752, "width":0.619584, "height":0.032149}, "confidence":0.646595}
 ] 
}
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...