-3

我在 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并使用正则表达式删除数字,但在执行此操作时没有成功。

帮助将不胜感激。

4

1 回答 1

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 数组,请跳过第一步和最后一步。

示范

于 2013-10-07T11:36:19.343 回答