在javascript中删除json键中的数字

时间:2013-10-07 11:33:19

标签: javascript regex json

我在JSON中找到以下输出:

[{
    "ApplicationView": "",
    "Styling": "",
    "minwidth": "450px",
    "minheight": "350px",
    "kendoWindowWidth": "90%",
    "kendoWindowHeight": "90%",
    "iconSize": "20px",
    "viewerToolColor": "#8dcaf7",
    "DefaultView": "fit To Screen",
    "Email": "",
    "To": "ToPerson",
    "Subject": "This file is exported from actual program.",
    "From": "FromPerson",
    "Body": "This file is exported from actual program.",
    "Content": "",
    "zoomIn": "",
    "Icon16": "ZoomIn.png",
    "Label17": "Zoom In",
    "Visibility18": "true",
    "zoomOut": "",
    "Icon20": "ZoomOut.png",
    "Label21": "Zoom Out",
    "Visibility22": "true",
    "rotateLeft": null,
    "Icon24": "RotateLeft.png",
    "Label25": "Rotate Left",
    "Visibility26": "true",
    "rotateRight": "",
    "Icon28": "RotateRight.png",
    "Label29": "Rotate Right",
    "Visibility30": "true",
    "fitToScreen": "",
    "Icon32": "fittoscreen.png",
    "Label33": "Fit to Screen or Double Click on Image",
    "Visibility34": "true",
    "fullScreen": "",
    "Icon36": "fullscreen.png",
    "Label37": "Full Screen or 2 Times Double Click on Image",
    "Visibility38": "true",
    "saveAs": "",
    "Icon40": "download.png",
    "Label41": "Full Screen or 2 Times Double Click on Image",
    "Visibility42": "true",
    "print": "",
    "Icon44": "print.png",
    "Label45": "Print",
    "Visibility46": "true",
    "email": "Email",
    "Icon48": "email.png",
    "Label49": "Send Mail",
    "Visibility50": "true"
}]

我想从JSON的键中删除数字

示例:icon48将是icon

请指导我这样做。

我试图在json循环中访问foreach的密钥并使用正则表达式删除数字,但在执行此操作时却没有成功。

帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

不要试图直接使用JSON字符串:真的应该在更改之前对其进行解析。

var arr1 = JSON.parse(yourJSON);
var arr2 = arr1.map(function(o1){
    var o2 = {};
    for (var key in o1) {
        o2[key.replace(/\d+/g,'')] = o1[key];
    }
    return o2;
});
var finalJSON = JSON.stringify(arr2);

如果您实际上没有任何JSON而只是简单的JavaScript数组,请跳过第一步和最后一步。

Demonstration