json Oracle PL/SQL - 删除特定字符的最后一个实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5569623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:49:14  来源:igfitidea点击:

Oracle PL/SQL - Remove Last Instance of a specific Character

jsonstringplsql

提问by Patrick

enter code hereI'm working on building a group of functions that return a json string based on the parameters and an internal select.

enter code here我正在构建一组基于参数和内部选择返回 json 字符串的函数。

The problem that I'm running into is as i'm looping through the cursor in pl/sql, building the structure of the json, it adds an extra comma at the end of the last node.

我遇到的问题是,当我在 pl/sql 中循环游标时,构建 json 的结构,它在最后一个节点的末尾添加了一个额外的逗号。

It parses properly but the html eval of the json fails because of that extra comma. Is it possible to remove a specific character (say the very last comma in a string) in pl/sql. I've looked at REPLACE, but i cannot find documentation on using REPLACE with specific instances of a character.

它解析正确,但由于额外的逗号,json 的 html eval 失败。是否可以删除 pl/sql.xml 中的特定字符(比如字符串中的最后一个逗号)?我查看了 REPLACE,但找不到有关将 REPLACE 与特定字符实例一起使用的文档。

The loop looks like:

循环看起来像:

FOR appGroup IN appGroups
LOOP

tsResults := tsResults || ' "Group" : { ';
tsResults := tsResults || ' "Id" : "' || appGroup.group_id || '", ';
tsResults := tsResults || '"Name" : "' || appGroupd.display_name || '" ';
tsResults := tsResults || ' }, ';

END LOOP;

It's that very last comma in the last row that is giving me the grief.

正是最后一行中的最后一个逗号让我感到悲伤。

Is there any way to kill the last one w/o grabbing all of the string characters before it, and all of the characters after it and putting them together?

有没有办法杀死最后一个而不抓取它之前的所有字符串字符,以及它之后的所有字符并将它们放在一起?

Any suggestions as to how I can get around this problem all together would be greatly appreciated as well.

任何关于我如何一起解决这个问题的建议也将不胜感激。

Update

更新

Using Cybernate's answer I was able to make some changes to get it to work... Originally immediately following the END LOOP; I had additional code adding more to the result string:

使用 Cyber​​nate 的答案,我能够进行一些更改以使其正常工作......最初紧随 END LOOP 之后;我有额外的代码向结果字符串添加更多内容:

tsResults := tsResults ||  '}] }';

Which properly terminated the groups array that I was working on...

它正确地终止了我正在处理的组数组...

I placed the following code before that line:

我在该行之前放置了以下代码:

tiLastComma := INSTR(tsResults, ',', -1);
tsResults := SUBSTR(tsResults, 1, tiLastComma - 1);

The json now is terminated properly and evals properly when used with jquery.

当与 jquery 一起使用时,json 现在已正确终止并正确评估。

回答by Chandu

You can use INSTR function to find the position of the last occurance of the "," and then use SUBSTRING.

您可以使用 INSTR 函数找到“,”最后出现的位置,然后使用 SUBSTRING。

 SELECT SUBSTR(tsResults , 1, INSTR(tsResults , ',', -1)-1)
    INTO tsResults 
    FROM dual;

If you don't think Regular Expression is an overkill for this then use this statement in your PL/SQL:

如果您不认为正则表达式对此有点矫枉过正,那么请在您的 PL/SQL 中使用以下语句:

 SELECT REGEXP_REPLACE(tsResults , '\s*,\s*$', '') 
    INTO tsResults 
    FROM dual;

回答by Tobi

Its easier to use the rtrimfunction. So after the loop:

它更容易使用该rtrim功能。所以在循环之后:

tsResults := rtrim( tsResults, ',' );

Then to terminate the JSON, add the closing curly bracket in the same statement:

然后要终止 JSON,请在同一语句中添加右大括号:

tsResults := rtrim( tsResults, ',' ) || '}';

回答by Aasish Arora

Tobi, I had a similar scenario, but remember with RTRIM it will remove all instances of the value you have prescribed in the function.

Tobi,我有一个类似的场景,但请记住,使用 RTRIM 它将删除您在函数中指定的值的所有实例。

For example:

例如:

I would like to trim 'X' from the end of the column selection. (LAST INSTANCE ONLY)

我想从列选择的末尾修剪“X”。(仅限最后一次)

Column Value = 'SOMEVALUEXX'

列值 = 'SOMEVALUEXX'

Now RTrim will return:'SOMEVALUE'

现在 RTrim 将返回:'SOMEVALUE'

It could be possible that you are after = 'SOMEVALUEX'

您可能在 = 'SOMEVALUEX' 之后

Chandu's answer is correct.

Chandu的回答是正确的。

Always check your business process.

始终检查您的业务流程。