Problems with accents and characters in JSON?

3

I am developing an application on android that gets data from a JSON that looks like this

[{
"title": "Titulo",
"image": "Imagen url",
"op": "op",
"autor": "Autor",
"fecha": "Noviembre 15, 2016",
"nota":  "ejemplo de nota"
}]

The problem is that in my application the accents and the "ñ" appear with a strange character with a question mark, and I'm almost sure that the problem is in the json, how do I solve it?

    
asked by SpaceSpace 16.11.2016 в 02:09
source

5 answers

1

You have to put utf-8 in Content-Type when calling the method.

For example:

HttpURLConnection httpurlconnection = (HttpURLConnection) urlConnection;
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
httpurlconnection.setDoOutput(true);
httpurlconnection.connect();
    
answered by 18.11.2016 в 18:34
1

You could also include the utf8_encode

  • EXAMPLE1:

  • $ text = 'Option';

  • $ var = utf8_encode ($ text);
  • echo $ var;

Or you could also use iconv

  • EXAMPLE2:

  • $ text = 'Option';

  • $ var = iconv ('ISO-8859-1', 'ASCII // TRANSLIT', ($ text));
  • echo $ var;
answered by 21.11.2016 в 07:44
0

You must verify that your database is set up in UTF-8 and also the file where you make the inserts to your DB is also configured that way.

When you send the data of your html or php to your database use utf8_encode and when you receive it utf8_decode

    
answered by 16.11.2016 в 02:37
0

You can use this option:

json_encode(
$data,
JSON_HEX_AMP |
JSON_HEX_QUOT |
JSON_HEX_APOS |
JSON_UNESCAPED_UNICODE |
JSON_UNESCAPED_SLASHES
);

That will help you, you can read the information on this here

I also recommend that you use this meta tag:

<meta charset="utf-8" />
    
answered by 16.11.2016 в 02:46
0

It is important to define how you get the values of your .JSON file, you must define what type of encoding the file has.

For example if you download the file created with enconding "UTF-8" you can define:

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

One option that is perhaps the simplest is that when you get your string that contains characters that belong to another encoding, you code them using URLEncoder.encode () :

try {
            String cadenaCodificada = URLEncoder.encode(myCadenaCaracteres, "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    
answered by 16.11.2016 в 19:26