File read/json
code: test.json
{
"id": 123,
"array": 1,2,3,
"nest": {
"a": 1,
"b": 2,
"c": 3
}
}
code: python
import json
def read_json_file(file_path):
try:
with open(file_path, 'r') as f:
return json.load(f)
except FileNotFoundError:
print(f"File not found: {file_path}")
return {}
except json.JSONDecodeError as e:
print(f"Invalid JSON format: {e}")
return {}
except Exception as e:
print(f"Unexpected error: {e}")
return {}
data = read_json_file('test.json')
print(data)
print(data'nest''b')