java将JSON写入文件会带来什么?
我有一个JSON文件,上面有不同的国家和语言名称等。我想把它拆分成我所需要的信息。例如,我想
[{
"name": {
"common": "Afghanistan",
"official": "Islamic Republic of Afghanistan",
"native": {
"common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
"official": "\u062f \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646 \u0627\u0633\u0644\u0627\u0645\u064a \u062c\u0645\u0647\u0648\u0631\u06cc\u062a"
}
},
"tld": [".af"],
"cca2": "AF",
"ccn3": "004",
"cca3": "AFG",
"currency": ["AFN"],
"callingCode": ["93"],
"capital": "Kabul",
"altSpellings": ["AF", "Af\u0121\u0101nist\u0101n"],
"relevance": "0",
"region": "Asia",
"subregion": "Southern Asia",
"nativeLanguage": "pus",
"languages": {
"prs": "Dari",
"pus": "Pashto",
"tuk": "Turkmen"
},
"translations": {
"cym": "Affganistan",
"deu": "Afghanistan",
"fra": "Afghanistan",
"hrv": "Afganistan",
"ita": "Afghanistan",
"jpn": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3",
"nld": "Afghanistan",
"rus": "\u0410\u0444\u0433\u0430\u043d\u0438\u0441\u0442\u0430\u043d",
"spa": "Afganist\u00e1n"
},
"latlng": [33, 65],
"demonym": "Afghan",
"borders": ["IRN", "PAK", "TKM", "UZB", "TJK", "CHN"],
"area": 652230
}, ...
进入
[{
"name": {
"common": "Afghanistan",
"native": {
"common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
}
},
"cca2": "AF"
}, ...
但当我尝试时
[{
"name": {
"common": "Afghanistan",
"native": {
"common": "?????????" <-- NOT WHAT I WANT
}
},
"cca2": "AF"
},
这是我用来去掉我不想要的东西的重要代码
byte[] encoded = Files.readAllBytes(Paths.get("countries.json"));
String JSONString = new String(encoded, Charset.forName("US-ASCII"));
...
Writer writer = new OutputStreamWriter(new FileOutputStream("countriesBetter.json"), "US-ASCII");
writer.write(javaObject.toString());
writer.close();
我不明白为什么它会把课文变成问号。我试过几个字符集,但都没用。当我使用UTF-8时,我得到اÙ�غانستان
请帮帮我。谢谢
# 1 楼答案
\u0627是unicode而非ascii,您不能用ascii表示阿拉伯字符-因此是?。有关utf格式之间的差异,请参见Difference between UTF-8 and UTF-16?
当你写UTF-8时,你需要以相同的编码读取,这样“记事本”就知道如何显示它拥有的字节。如果使用这种编码将其读回java,它将不会被改变
# 2 楼答案
您需要更改控制台编码才能看到这一点
进入跑步>运行配置
将打开一个弹出窗口。选择“常用”选项卡。在编码部分,选择其他,然后在下拉列表中选择UTF-8
现在运行程序。我得到了以下结果: