JSON does not accept accentuation

Asked

Viewed 11,772 times

4

Friends, I have a problem, the string that has accent not being able to catch it using JSON:

$imagens = array();
$sql = mysql_query("SELECT * FROM texto_index");
while ($row = mysql_fetch_row($sql)){
      $imagens[] = $row;
}
echo json_encode($imagens);

for example: Prescription comes as null

the array:

Array ( [0] => Array ( [0] => 5 [1] => RECEITUÁRIOS [2] => MASCULINOS [3] => 1 [4] => #ffffff [5] => 56 [6] => 28 [7] => Direita [8] => cache/1404135032s-receituario-feminino-eco2.jpg ) [1] => Array ( [0] => 6 [1] => ÓCULOS [2] => MASCULINOS [3] => 1 [4] => #ffffff [5] => 52 [6] => 20 [7] => esquerda [8] => cache/1404225957262.jpg ) )
  • Possible duplicate. Is not exactly but solves the same problem, only otherwise.

  • @Brunoaugusto I believe not to be because of the language!

  • In addition to the json conversion error, the source also influenced @Furlan?

  • @user6026, the term "language" is different from the term "Encode/charset". Normally, the problem according to what you presented is related to the type of Encode/charset of the data and PHP scripts. Then the first step is to set up the environment and the data for an appropriate Encounter. Recommended is UTF-8. If you really want to solve it the right way, click on the environment. Otherwise, you can solve it by using htmlentities and utf_encode, etc.. In this case it will only make worse what is already bad.. rsr you are the one who chooses.

2 answers

5


Put in your first line of your code like this:

header("Content-Type: text/html; charset=UTF-8",true)

and after the connection put this line

mysql_set_charset('utf8');
$imagens = array();
$sql = mysql_query("SELECT * FROM texto_index");
while ($row = mysql_fetch_row($sql)){
  $imagens[] = $row;
}
echo json_encode($imagens);

Preferably always use UTF-8 by default

They can be made that way too:

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

Example on doubt Json notation: Ideone

  • @Harrypotter [http://www.planow.com.br/eco]

  • 1

    I think this is the source of css! I made an example here to see and ta right!!! give a look at your CSS now this font should not have accent, good I think !!!

  • 1

    I just checked your css font has no accent, check it now... ! json ta right...

0

When using string in the ISO-8859-1(latin1) charset, the json_encode function of php presents problems, cutting the value after the first accented character.

To avoid this problem, before converting to json, simply convert the string to UTF-8.

In php, to perform this conversion, just use the utf8_encode function.

Similarly, this type of problem must occur with other character encodings than UTF-8. It is therefore recommended to always use UTF-8, especially when you need to act with native PHP functions that work with JSON, both json_encode and json_decode.

try a test as follows and see if it solves your problem:

<?php 

    $imagens = array();
    $sql = mysql_query("SELECT * FROM texto_index");
    while ($row = mysql_fetch_row($sql)){
        $imagens['id']   = $row[0];
        $imagens['n1']   = utf8_encode($rom[1]);
        $imagens['n2']   = utf8_encode($rom[2]);
        $imagens['n3']   = $row[3];
        $imagens['cor']  = $row[4];
        $imagens['w']    = $row[5];
        $imagens['h']    = $row[6];
        $imagens['lado'] = $row[7];
        $imagens['link'] = $row[8];
    }
    echo json_encode($imagens);

?>
  • returned this: ["Array","Array"]

  • You can give a print_r($images); before echo json_encode() and paste here the result just so I can see what it generates for that array?

  • Array ( [0] => Array ( [0] => 5 [1] => RECEIPTS [2] => MASCULINE [3] => 1 [4] => #ffffff [5] => 56 [6] => 28 [7] => Right [8] => cache/1404135032s-female-eco2.jpg ) [1] => Array ( [0] => 6 [1] => GLASSES [2] => MEN [3] => 1 [4] => #ffffff [5] => 52 [6] => 20 [7] => left [8] => cache/1404225957262.jpg ) )

  • @Furlan, please add this kind of details on question.

  • I understood now what happens... I edited my post for you to take a look giving a suggestion to create indices for the images array and defining the utf8 for what really is a text... the indices I created are suggestive, vc change according to what you need.... These indices will also facilitate the use of json when you have q access to certain information

Browser other questions tagged

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