Reputation: 1446
How can I parse special characters in JSON? And since I'm making a German App, it includes a lots of special characters like ä,ö,ü,ß. How can I show those characters through parsing JSON? Right now they are only shown as '?'
Here's my JSON parsing method:
void examineJSONFile()
{
try
{
String y = "";
InputStream is = this.getResources().openRawResource(R.raw.json);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8")
);
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String jsontext = writer.toString();
JSONArray entries = new JSONArray(jsontext);
int j;
for (j=0;j<entries.length();j++)
{
JSONObject post = entries.getJSONObject(j);
y += post.getString("description") + "\n";
}
txt_beschreibung.setText(y);
}
catch (Exception je)
{
txt_beschreibung.setText("Error w/file: " + je.getMessage());
}
}
Upvotes: 2
Views: 10764
Reputation: 596
first check encoding type in xml, either UTF-8 or ISO-8859-1
if encoding type is ISO-8859-1, then in programme just change this code for read special character
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(this);
InputSource is = new InputSource(xmlUrl.openStream());
is.setEncoding("ISO-8859-1");
xmlReader.parse(is);
Upvotes: 3
Reputation: 10517
Such chars must be in UTF-8 encoding at least. Check if your file is saved in this incoding.
Upvotes: 4