How can I save a json-encoded string with international characters to the databse and then parse the decoded string in the browser?

<?php           
    $string = "très agréable";  
    // to the database 
    $j_encoded = json_encode(utf8_encode($string)); 
    // get from Database 
    $j_decoded = json_decode($j_encoded); 
?>    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <?= $j_decoded ?>
</html> 

推荐答案

This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.

流程的每个部分都需要使用UTF-8编码.

  • 数据库连接

  • 数据库表

  • Your PHP file (if you are using special characters inside that file as shown in your example above)

  • 您输出的content-type个标题

Json相关问答推荐

解析JSON说函数parse_json不存在?

如何判断响应数组是否存在以及S是否有其他内容...?

解析SQL中的嵌套JSON

将=分隔值文件转换为:json文件

如何使用 SQL Server 将 json 存储为字符串的列分解/规范化为行和列?

是否可以在有条件的情况下将 json 对象转换为 JOLT 中的数组?

boost::json::value 的大括号初始化将其从对象转换为数组

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

杰克逊 2.0 和 Spring 3.1

将 YAML 文件转换为 Python JSON 对象

如何判断字符串是否为json格式

如何一次加载无限滚动中的所有条目以解析python中的HTML

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

如何在 Django JSONField 数据上聚合(最小/最大等)?

使用 Python 3 读取 JSON 文件

as_json 没有在关联上调用 as_json

与classic 规范化表相比,postgres JSON 索引是否足够高效?

使用 Codable 序列化为 JSON 时的 Swift 字符串转义

SCRIPT5009:JSON未定义

如何在本地存储中存储对象数组?