How to put in JSON Object non-english letters?

2019-09-02 12:18发布

问题:

I am android developer. And I get my data from MySQL database via PHP code:

<?php

    $con = mysqli_connect("..........", "...........", "........", "......");

  $query = mysqli_query($con,
           "SELECT * FROM news WHERE status = 'actual' ORDER BY id DESC");
$someArray = [];
$someArray;

  // Loop through query and push results into $someArray;
  while ($row = mysqli_fetch_assoc($query)) {
    array_push($someArray, [
        'id' => $row['id'],
'title' => $row['title'],
'text' => $row['text'],
'date' =>$row['date']
 ]);
  }

  $someJSON = json_encode($someArray);

echo  $someJSON;
?>

and it works for data with english letters , but when i tried to add in my database russian letters or letters like "ə ç ı ş ü ö ğ and etc." - it is not working. I have researched and found that i must add this:

<meta charset="utf-8"/>

at the top of PHP code, but it still not working, then i was researching more and found that I should correct $someJSON , and make it look like that:

  $someJSON = json_encode($someArray, JSON_UNESCAPED_UNICODE);

and it still not working

the part with russian letters looks like that :

{"id":"4","title":"????????","text":"????????? ??????? ?????, ? ??? ????????? ?? ?????????","date":"21.08.18"}

in my database for "title" it is :utf8mb4_unicode_520_ci for "text" it is : utf8_unicode_520_ci
(I don't know difference just make it different in case if one of them works)

How to make it show russian letters(and other non-english) with JSON Object?

回答1:

$someJSON = json_encode($someArray, JSON_UNESCAPED_UNICODE);

is fine. The problem is elsewhere. See "question mark" in here

The most likely cause/fix:

The column in the database table is not CHARACTER SET utf8 (or utf8mb4). Fix this. (Verify with SHOW CREATE TABLE.)

Note: the JSON really has question marks; the data cannot be recovered from it. You must go back and rebuild the JSON.