我有一个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时,我得到اÙ�غانستان
请帮帮我。谢谢。
答案 0 :(得分:1)
\ u0627是unicode而不是ascii,你不能代表ascii中的阿拉伯字符 - 因此?有关utf格式之间的差异,请参阅Difference between UTF-8 and UTF-16?
当你把它写成UTF-8时,你需要读相同的编码,这样“记事本”知道如何显示它有的字节。如果你使用该编码将其读回java,它将不会改变。
答案 1 :(得分:0)
您需要更改控制台编码才能看到此内容。
转到运行&gt; 运行配置
弹出窗口将会打开。选择常用标签。在编码部分中,选择其他,然后在下拉列表中选择 UTF-8 。
现在运行该程序。我得到了以下结果:
[ {
"name" : {
"common" : "Afghanistan",
"official" : "Islamic Republic of Afghanistan",
"natives" : {
"common" : "افغانستان",
"official" : "د افغانستان اسلامي جمهوریت"
}
},
"tld" : [ ".af" ],
"cca2" : "AF",
"ccn3" : "004",
"cca3" : "AFG",
"currency" : [ "AFN" ],
"callingCode" : [ "93" ],
"capital" : "Kabul",
"altSpellings" : [ "AF", "Afġānistān" ],
"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" : "アフガニスタン",
"nld" : "Afghanistan",
"rus" : "Афганистан",
"spa" : "Afganistán"
},
"latlng" : [ 33, 65 ],
"demonym" : "Afghan",
"borders" : [ "IRN", "PAK", "TKM", "UZB", "TJK", "CHN" ],
"area" : 652230
} ]