javascript JSON 从最后一个对象中删除尾随逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34344328/
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-10-28 17:35:16  来源:igfitidea点击:

JSON Remove trailiing comma from last object

javascriptjsonregex

提问by mrtonyb

This JSON data is being dynamically inserted into a template I'm working on. I'm trying to remove the trailing comma from the list of objects.

这个 JSON 数据被动态插入到我正在处理的模板中。我正在尝试从对象列表中删除尾随逗号。

The CMS I'm working in uses Velocity, which I'm not too familiar with yet. So I was looking to write a snippet of JavaScript that detects that trailing comma on the last object (ITEM2) and removes it. Is there a REGEX I can use to detect any comma before that closing bracket?

我正在使用的 CMS 使用 Velocity,我还不太熟悉它。所以我想写一段 JavaScript 代码来检测最后一个对象 (ITEM2) 上的尾随逗号并将其删除。是否有一个 REGEX 我可以用来检测右括号之前的任何逗号?

[  
   {  
      "ITEM1":{  
         "names":[  
            "nameA"
         ]
      }
   },
   {  
      "ITEM2":{  
         "names":[  
            "nameB",
            "nameC"
         ]
      }
   }, // need to remove this comma!
]

回答by Dmitry Parzhitsky

You need to find ,, after which there is no any new attribute, object or array.
New attribute could start either with quotes ("or ') or with any word-character (\w).
New object could start only with character {.
New array could start only with character [.
New attribute, object or array could be placed after a bunch of space-like symbols (\s).

您需要找到,,之后没有任何新的属性、对象或数组。
新属性可以以引号 ("') 或任何单词字符 ( \w)开头。
新对象只能以字符开头{
新数组只能以字符开头[
新的属性、对象或数组可以放在一堆类似空格的符号 ( \s) 之后。

So, the regex will be like this:

所以,正则表达式将是这样的:

let regex = /\,(?!\s*?[\{\[\"\'\w])/g;

Use it like this:

像这样使用它:

// javascript
let input; // this is the initial string of data
let correct = input.replace(regex, ''); // remove all trailing commas
let data = JSON.parse(correct); // build a new JSON object based on correct string

Try the first regex.

尝试第一个正则表达式



Another approachis to find every ,, after which there is a closing bracket.
Closing brackets in this case are }and ].
Again, closing brackets might be placed after a bunch of space-like symbols (\s).

另一种方法是找到 every ,,然后有一个右括号。
在这种情况下,右括号是}]
同样,右括号可能放在一堆类似空格的符号 ( \s) 之后。

Hence the regexp:

因此正则表达式:

let regex = /\,(?=\s*?[\}\]])/g;

Usage is the same.

用法是一样的。

Try the second regex.

尝试第二个正则表达式

回答by Federico Piazza

For your specific example, you can do a simple search/replace like this:

对于您的具体示例,您可以像这样进行简单的搜索/替换:

,\n]$

Replacement string:

替换字符串:

\n]

Working demo

工作演示

Code

代码

var re = /,\n]$/; 
var str = '[  \n   {  \n      "ITEM1":{  \n         "names":[  \n            "nameA"\n         ]\n      }\n   },\n   {  \n      "ITEM2":{  \n         "names":[  \n            "nameB",\n            "nameC"\n         ]\n      }\n   },\n]';
var subst = '\n]'; 

var result = str.replace(re, subst);

回答by Akshay Pethani

I also faced the same problem in the velocity template.

我在速度模板中也遇到了同样的问题。

You stated that the objects are dynamically inserted in the JSON. So most probably this should be the loop. So you can add the dummy object at the end of the loop.

您说对象是动态插入到 JSON 中的。所以很可能这应该是循环。所以你可以在循环的末尾添加虚拟对象。

Your velocity template should look like this:

您的速度模板应如下所示:

   {
  "data": [
  #foreach($Student in $listStudent)
    {      
        "id" : "$Student.id"
        "Name" : "$Student.name"

    },
  #end
  {"_COMMENT":"THIS IS PLACED HERE JUST TO EGNORE TRAILING COMMA AT THE END OF LAST OBJECT",
   "_COMMENT":"THIS OBJECT MUST IGNORE WHILE PARSING"
  }
  ]
}

The only disadvantage of this trick is that you have to write some logic at the client end to avoid the dummy object while parsing the JSON object.

这个技巧的唯一缺点是你必须在客户端编写一些逻辑来避免在解析 JSON 对象时出现虚拟对象。