You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python加载单个JSON文件报JSONDecodeError,其余文件正常求助

Troubleshooting the JSONDecodeError for Your Specific File

Hey there, let's get to the bottom of why that single JSON file is failing to load while all others work perfectly. The JSONDecodeError you're seeing points directly to a syntax issue (or possibly an encoding quirk) in that specific file—your code looks solid for handling the rest of the files, so we can focus on fixing the problematic JSON itself.

Step 1: Pinpoint the Exact Problem File & Verify the Error

First, let's update your code to explicitly catch and report which file is causing the error (in case you haven't already narrowed it down). This will make it easier to target your debugging:

import os
import json

folder_path = 'C:/Users/yilin.chen/Desktop/JSON'
for filename in os.listdir(folder_path):
    if filename.endswith(".json"):
        file_path = os.path.join(folder_path, filename)
        try:
            # Use a with-statement for safer file handling, and specify encoding to rule out issues
            with open(file_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
            print(f"✅ Successfully loaded {filename}")
        except json.JSONDecodeError as e:
            print(f"❌ Error loading {filename}: {e}")

Running this will confirm the exact filename, and repeat the error location (line 384, column 25) for that file.

Step 2: Inspect the Problematic Line in the JSON File

Open the failing JSON file in a text editor (like VS Code, Notepad++, or even plain Notepad) and jump to line 384, column 25. Here are the most common issues you'll find there:

  • Missing or mismatched quotes: JSON requires double quotes (") for keys and string values—single quotes (') are invalid. Check if a key/value is missing quotes, or if there's an unclosed quote.
  • Trailing comma: If the line is part of a list or object, make sure there's no extra comma after the last element (e.g., {"key": "value",} is invalid).
  • Unescaped characters: If there's a quote inside a string (like "message: "Hello!""), it needs to be escaped with a backslash: "message: \"Hello!\"".
  • Invisible control characters: Sometimes files can have hidden characters (like non-printable whitespace) that break JSON parsing. Try deleting and retyping the characters around column 25 to fix this.

Step 3: Validate the JSON (Without External Tools)

If you want to confirm the entire file's validity without leaving your terminal, use Python's built-in JSON validator tool. Run this command in your command prompt, replacing problem_file.json with the actual filename:

python -m json.tool "C:/Users/yilin.chen/Desktop/JSON/problem_file.json"

This will either print the formatted JSON (if it's valid) or throw a detailed error pointing to the exact issue.

Step 4: Fix Encoding Issues (If Needed)

If the file uses a non-UTF-8 encoding (like GBK or Latin-1), the default open() call might misread it, causing parsing errors. Try specifying the correct encoding when opening the file:

with open(file_path, 'r', encoding='gbk') as f:
    data = json.load(f)

You can usually find a file's encoding using tools like Notepad++ (look in the "Encoding" menu).

Once you fix the syntax or encoding issue in the problematic file, your original code should load it without errors!

内容的提问来源于stack exchange,提问作者Drake .C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:34:04