2
I have a JSON Array
and when it is displayed in a ListView
on Android, appears with special characters:
JSON generated by php:
[{"id":"1","titulo":"X-Burg","descricao":"Hambúrguer,...","preco":"R$ 7,50","tipo":"0"},{"id":"2","titulo":"Dogão","descricao":"Salsicha,...:"R$ 7,50","tipo":"0"}...]
Code Java
abridged:
Convert:
private CardapioEntities convertCardapio(JSONObject obj) throws JSONException {
String titulo = obj.getString(TAG_TITULO);
String descricao = obj.getString(TAG_DESCRICAO);
String preco = obj.getString(TAG_PRECO);
String tipo = obj.getString(TAG_TIPO);
return new CardapioEntities(titulo, descricao, preco, tipo);
}
Catch the JSON
and play in the ListView
private void _getCardapio(String result) {
try {
JSONArray json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
CardapioEntities cardapioEntities = new CardapioEntities();
if (convertCardapio(json.getJSONObject(i)).getCardapioTipo().contains("0")) {
cardapioEntities.setCardapioTitulo(convertCardapio(json.getJSONObject(i)).getCardapioTitulo());
cardapioEntities.setCardapioDescricao(convertCardapio(json.getJSONObject(i)).getCardapioDescricao());
cardapioEntities.setCardapioPreco(convertCardapio(json.getJSONObject(i)).getCardapioPreco());
listaLanches.add(cardapioEntities);
}
}
final ListView lvLanches = (ListView) findViewById(R.id.lvLanches);
lvLanches.setAdapter(new CustomListAdapter(this, listaLanches));
} catch (JSONException e) {
e.printStackTrace();
}
}
Tips and examples of improvements are always welcome.
see this http://stackoverflow.com/questions/16324956/android-convert-json-to-uft-8
– Igor Ronner