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

Java特殊字符编码问题:JSON读取后乱码求助

Fixing Garbled UTF-8 Characters When Reading JSON in Java

Hey there! Let's break down why your name Martin Perénya is showing up as Martin Perénya—it's all about character encoding mismatches. Here's what's happening and how to fix it:

The Root Cause

Your FileReader is using your operating system's default character set (like GBK on Windows) to read the raw.json file, but your JSON file is almost certainly saved in UTF-8 (the universal standard for JSON). When the reader encounters UTF-8 characters like é, it misinterprets them using the wrong encoding, leading to that garbled text.

The Fix

Replace FileReader with InputStreamReader and explicitly specify UTF-8 encoding. This ensures your code reads the file correctly no matter what system it's running on.

Modified Code

Here's your updated getAll() method with the fix and some null-safety improvements to avoid common pitfalls:

package main.service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Service {
    public String getAll() throws FileNotFoundException {
        JsonParser jsonParser = new JsonParser();
        // Replace FileReader with InputStreamReader to enforce UTF-8 encoding
        InputStreamReader reader = new InputStreamReader(
            new FileInputStream("raw.json"), 
            StandardCharsets.UTF_8
        );
        
        JsonObject data = (JsonObject) jsonParser.parse(reader);
        JsonArray issues = data.get("issues").getAsJsonArray();
        JsonArray processedIssues = new JsonArray();

        for(JsonElement i : issues) {
            JsonObject object = new JsonObject();
            JsonObject issue = (JsonObject) i;
            JsonObject fields = issue.get("fields").getAsJsonObject();
            
            // Handle null cases first to avoid NullPointerExceptions
            JsonObject assignee = fields.get("assignee").getAsJsonObject();
            String name = "None";
            
            if (assignee != null && assignee.size() > 0) {
                JsonElement nameElement = assignee.get("displayName");
                if (nameElement != null && !nameElement.getAsString().isEmpty()) {
                    name = nameElement.getAsString();
                }
            }
            
            object.addProperty("name", name);
            processedIssues.add(object);
        }

        JsonObject returnData = new JsonObject();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format.format(new Date());
        returnData.addProperty("created", dateString);
        returnData.add("featureList", processedIssues);
        
        System.out.println("returnedData - " + returnData);
        return returnData.toString();
    }
}

Key Changes Explained

  • InputStreamReader with UTF-8: This forces the file to be read using the correct encoding for JSON, which directly fixes the garbled character issue.
  • Null safety checks: Added guards for assignee being null and displayName being empty to prevent crashes—something every new Java dev should get in the habit of doing!

Quick Additional Checks

  • Double-check your raw.json file is saved in UTF-8: Open it in a text editor like Notepad++ and confirm the encoding setting (look in the "Encoding" menu).
  • If you later write this data to a file, make sure to use UTF-8 there too (e.g., OutputStreamWriter with StandardCharsets.UTF_8).

内容的提问来源于stack exchange,提问作者Lynskey08

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 23:37:44