如何放入JSON对象非英文字母

如何放入JSON对象非英文字母

本文介绍了如何放入JSON对象非英文字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发人员.然后通过PHP代码从MySQL数据库中获取数据:

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"/>

位于PHP代码的顶部,但仍然无法正常工作,然后我进行了更多研究,发现我应该更正$someJSON,并使它看起来像这样:

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

它仍然不起作用

带有俄语字母的部分看起来像这样:

the part with russian letters looks like that :

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

在我的数据库中的"title"是:utf8mb4_unicode_520_ci对于文本",它是:utf8_unicode_520_ci
(我不知道区别只是在其中之一起作用的情况下使其有所不同)

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

如何使其与JSON对象一起显示俄语字母(和其他非英语的字母)?

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

推荐答案

$someJSON = json_encode($someArray, JSON_UNESCAPED_UNICODE);

很好.问题出在其他地方.请参见 这里

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

最可能的原因/解决方法:

The most likely cause/fix:

数据库表中的列不是CHARACTER SET utf8(或utf8mb4).解决这个问题. (用SHOW CREATE TABLE验证.)

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

注意:JSON确实带有问号;无法从中恢复数据.您必须返回并重建JSON.

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

这篇关于如何放入JSON对象非英文字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:50