删除Json键中的空格 [英] Remove spaces in Json keys
本文介绍了删除Json键中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的json格式如下
[{
"Id": "ALFKI",
"Contact Name": "Maria Anders",
"Contact Title": "Sales Representative",
"City": "Berlin",
"Slider": 10
}, {
"Id": "ANATR",
"Contact Name": "Ana Trujillo",
"Contact Title": "Owner",
"City": "México D.F.",
"Slider": 5
}]
我想要的Json
[{
"Id": "ALFKI",
"ContactName": "Maria Anders",
"ContactTitle": "Sales Representative",
"City": "Berlin",
"Slider": 10
}, {
"Id": "ANATR",
"ContactName": "Ana Trujillo",
"ContactTitle": "Owner",
"City": "México D.F.",
"Slider": 5
}]
Kendo grid doesn不接受带空格的键名
Kendo grid doesn't accept Key names with spaces
推荐答案
你可以使用 JSON.stringi fy()
, JSON.parse()
, String.prototype.replace()
使用 RegExp
/ \s(?= \w +:) / g
匹配空格字符后跟一个或更多单词字符后跟后跟
:
You can use JSON.stringify()
, JSON.parse()
, String.prototype.replace()
with RegExp
/\s(?=\w+":)/g
to match space character followed by one or more word characters followed by "
followed by :
var arr = [{
"Id": "ALFKI",
"Contact Name": "Maria Anders",
"Contact Title": "Sales Representative",
"City": "Berlin",
"Slider": 10
}, {
"Id": "ANATR",
"Contact Name": "Ana Trujillo",
"Contact Title": "Owner",
"City": "México D.F.",
"Slider": 5
}];
arr = JSON.parse(JSON.stringify(arr).replace(/\s(?=\w+":)/g, ""));
console.log(arr);
这篇关于删除Json键中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文