Convert JSON to UTF-8 on Android

Asked

Viewed 348 times

2

I have a JSON Array and when it is displayed in a ListView on Android, appears with special characters:

Exemplo

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

2 answers

2


I solved it, the problem was here:

BufferedReader buffer = new BufferedReader(new InputStreamReader(content, "iso-8859-1"), 8);                     

I added the iso-8859-1 and worked!

Reference: https://stackoverflow.com/a/14949076/3456409

1

Instead of using Jsonobject and taking data, use the GSON library referencing your object that will receive all the data, remembering that your object( Cardapioentities ) should have its attributes with the same name that comes from json.

For example Voce could create an object that contains a menu list.

public class ListaCardapioEntities {

  private List<CardapioEntities> cardapioEntitiesList;

  //construtor
  //get set 

}

 private ListaCardapioEntities convertCardapio(String json) throws JSONException {
     Gson gson = new Gson();
    ListaCardapioEntities listaCardapioEntities = gson.fromJson( json, ListaCardapioEntities.class );

    return listaCardapioEntities;
}

And in your json put it like this

{ "cardapioEntitiesList" : [{"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"}]}

And on the charset is UTF-8 ? If not then try putting it.

String resposta = EntityUtils.toString(httpEntity, "UTF-8");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.