dskpywvki951583595 2018-08-21 12:56
浏览 82

如何输入JSON Object非英文字母?

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条回答 默认 最新

  • doudai8783 2018-08-23 18:55
    关注
    $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.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊