从包含文本 MariaDB 的 sql 查询中获取 JSON
Get JSON from sql query that contains text MariaDB
假设我在 SQL table 中有一个名为“天气”的行,例如 ..
"今天天气晴朗{"temp":"79","rainchance":"0%"}"
我如何才能只获得 json 数据?
使用 MariaDB
我试过 'JSON_VALUE(weather,'$.temp') 作为温度',但这似乎不起作用,因为 JSON 数据之前的字符串。
还在学习中,谢谢!
您可以结合使用 substring() 和 instr() 来删除 JSON 部分。即:
create table test (MyStr text);
insert into test (MyStr)
values ('Weather today is Sunny{"temp":"79","rainchance":"0%"}');
select JSON_VALUE(substring(mystr, instr(mystr,'{')),'$.temp')
from test;
PS: 如果它没有以'}'结尾,那么你可以使用substring_index()来得到想要的部分。
假设我在 SQL table 中有一个名为“天气”的行,例如 .. "今天天气晴朗{"temp":"79","rainchance":"0%"}"
我如何才能只获得 json 数据? 使用 MariaDB
我试过 'JSON_VALUE(weather,'$.temp') 作为温度',但这似乎不起作用,因为 JSON 数据之前的字符串。
还在学习中,谢谢!
您可以结合使用 substring() 和 instr() 来删除 JSON 部分。即:
create table test (MyStr text);
insert into test (MyStr)
values ('Weather today is Sunny{"temp":"79","rainchance":"0%"}');
select JSON_VALUE(substring(mystr, instr(mystr,'{')),'$.temp')
from test;
PS: 如果它没有以'}'结尾,那么你可以使用substring_index()来得到想要的部分。