我模型的一个属性是JSON类型.如果我使用希腊字符('μ'等)在JSON中,它们在MariaDB中被自动编码为unicode字符(例如,'μ'被键入为'\03bc').当我使用Eloquent进行读取时,数据会自动转换为有效字符.然而,如果我想通过'whereJsonContainsKey'在JSON中搜索包含字符'μ'的键,它找不到任何记录.对于常规字符,数据是正常搜索的.如何使用非标准字符搜索行?

我正在以这种方式创建数据:

$unit = Unit::factory()->create();
        $unit->name = 'Some name';
        $unit->unit = 'Ω';
        $unit->prefixes = ['μ' => 0.000001, 'k' => 1000, 'M' => 1000000];
        $unit->save();

then when I wand to fetch:
$u = Unit::whereJsonContainsKey('prefixes->μ')->first(); $u = null, but when
$u = Unit::whereJsonContainsKey('prefixes->k')->first(); the data is fetched correctly.

推荐答案

The problem is based on the fact that during json encoding, unicode multibyte characters are encoded using \uXXXX by default. This behavior is based on the json_encode function provided by php itself.
As statet in the documention for json_encode, the function accepts a bunch of flags in form of a bitmask where one of this flags is JSON_UNESCAPED_UNICODE.

JSON_UNESCAPED_UNICODE (int)
Encode multibyte Unicode characters literally (default is to escape as \uXXXX).

一个可行的解决方案是强制json_encode通过使用上面的标志(如json_encode($value, JSON_UNESCAPED_UNICODE))来编码多字节unicode字符.

Json相关问答推荐

如何使用表键名称GROUP_BY

如何循环访问多个子数组并在单个JSON中检索数据点

ArcGIS json到Geojson的变换

JQ-JSON将键转换为对象

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

使用Kotlin限制序列化类属性的允许整数值

JOLT转换以基于对象属性过滤JSON数组

使用 jq,如何将两个属性构成键的对象数组转换为对象的索引对象?

Python 将 struct 化文本转换和筛选为对象

使用 jq 工具将文本从 txt 文件转换为 json

如何使用 jq 在连续的 json 记录流上调用操作

颠簸转换问题

派生类的属性没有得到价值

Powershell 7.2:ConvertFrom-Json - 日期处理

如何在linux中用jq过滤json数组?

在 rust 中从 API 反序列化 serde_json

在PowerShell中按时间戳过滤JSON

反序列化大型 json 对象的 JsonMaxLength 异常

是否可以将数据写入本地 json 文件,除了Angular 之外什么都没有?

有没有办法在 angular.json 中扩展配置?