Java特殊字符编码问题:JSON读取后乱码求助
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
InputStreamReaderwith 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
assigneebeing null anddisplayNamebeing empty to prevent crashes—something every new Java dev should get in the habit of doing!
Quick Additional Checks
- Double-check your
raw.jsonfile 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.,
OutputStreamWriterwithStandardCharsets.UTF_8).
内容的提问来源于stack exchange,提问作者Lynskey08

