我将字符串{"abc":"dds","def":null,"ghi":fgi"}
存储在数据库的字段中。如何在" def"之后找到下一个逗号。字符串?
其实我有这样的字符串列表,例如
{"abc":"dds","def":null,"ghi":"fgi"}
{"abc":"123","def":234,"ghi":"fgd"}
{"abc":"133-d","def":"asd-123","ghi":"fgi"}
.....
我需要删除所有出现的" def"有价值的,我需要结果为
{"abc":"dds","ghi":"fgi"}
{"abc":"123","ghi":"fgd"}
{"abc":"133-d","ghi":"fgi"}
答案 0 :(得分:1)
我根据您提供的数据给您一个工作示例。
var $li = $('#main-menu li').on({
mouseenter: function() {
console.log("mouse over: " + $li.index(this))
},
mouseleave: function() {
console.log("mouse leave: " + $li.index(this))
}
});
SET @str := '{"abc":"133-d","def":"asd-123","ghi":"fgi"}';
<强>解释强>
SELECT
CONCAT(
SUBSTRING(
@str
FROM
1 FOR LOCATE('def' ,@str) - 2
),
SUBSTRING(
@str
FROM (LOCATE('def' ,@str) + LOCATE(',',SUBSTRING(@str,LOCATE('def' ,@str)))) FOR (LENGTH(@str) + 1 - (LOCATE('def' ,@str) + LOCATE(',',SUBSTRING(@str,LOCATE('def' ,@str)))))
)
) AS newJson;
Explanation based on this string '{"abc":"dds","def":null,"ghi":fgi"}'
现在连续结果#1和结果#4得到完整的所需字符串{“abc”:“dds”,“ghi”:fgi“}
注意:在应用程序级别执行此作业肯定会为您提供更大的灵活性。
修改强>
您可以创建一个名为SELECT SUBSTRING(@str FROM 1 FOR LOCATE('def' ,@str) - 2 );
Result#1: {"abc":"dds",
SELECT (LOCATE('def' ,@str) + LOCATE(',',SUBSTRING(@str,LOCATE('def' ,@str))));
Result#2: 25 // Location of the comma next to 'def':value
SELECT (LENGTH(@str) + 1 - (LOCATE('def' ,@str) + LOCATE(',',SUBSTRING(@str,LOCATE('def' ,@str)))));
Result#3: 11 // Length of the rest of the string after that very comma
Now get the SUBSTRING(@str FROM position 25 to Result#3 )
It will return
Result #4: "ghi":fgi"}
的函数,并在需要的地方使用它。
getNewJsonString
示例函数调用:
DELIMITER $
DROP FUNCTION IF EXISTS getNewJsonString$
CREATE FUNCTION getNewJsonString(inputStr VARCHAR(300))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE returnString VARCHAR(255);
SET @str := inputStr;
SET returnString := (SELECT
CONCAT(
SUBSTRING(
@str
FROM
1 FOR LOCATE('def' ,@str) - 2
),
SUBSTRING(
@str
FROM (LOCATE('def' ,@str) + LOCATE(',',SUBSTRING(@str,LOCATE('def' ,@str)))) FOR (LENGTH(@str) + 1 - (LOCATE('def' ,@str) + LOCATE(',',SUBSTRING(@str,LOCATE('def' ,@str)))))
)
)) ;
RETURN returnString;
END$
DELIMITER ;
<强>输出:强>
SELECT getNewJsonString('{"abc":"dds","def":null,"ghi":"fgi"}');