This problem appears when data are encoded with UTF_8
charset but you are using windows-1252 (or ISO-8859-1) to read them. I created JSON
file encoded in UTF-8
:
{
"value": "Verfügung"
}
And read it as a Map
using below code:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class GsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
Gson gson = new GsonBuilder().create();
Charset outputEncoding = Charset.forName("windows-1252");
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(jsonFile), outputEncoding)) {
Map map = gson.fromJson(reader, Map.class);
System.out.println(map);
}
byte[] bytes = "Verfügung".getBytes(StandardCharsets.UTF_8);
System.out.println(new String(bytes, outputEncoding));
}
}
Above app prints:
{value=Verfügung}
Verfügung
GsonApp
file is also encoded in UTF-8
.
I guess, in your case you read UTF-8
encoded data with default system charset which is probably windows-1252
. You load data from DB
so you probably need to set explicitly encoding to UTF-8
in connection string. See example for MySQL
database: JDBC character encoding.
See also: