Convert JSON to UTF-8 on Android

2

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

JSONgeneratedbyphp:

[{"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 summarized:

Converter:

 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);
    }

Get JSON and play on 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.

    
asked by anonymous 03.09.2014 / 14:09

2 answers

2

I solved, the problem was here:

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

I added iso-8859-1 and it worked!

  

Reference: link

    
03.09.2014 / 17:05
1

Instead of using JSONObject and taking data as given, use the GSON library referencing your object that would receive all the data, remembering that your object (CardapioEntities) should have its attributes with the same name that comes from json.

For example you 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"}]}

What about charSet is UTF-8? If not then try putting it in.

String resposta = EntityUtils.toString(httpEntity, "UTF-8");
    
03.09.2014 / 17:24