将递增数字添加到JSON对象String以使它们唯一

时间:2013-01-13 01:18:23

标签: javascript regex json string object

我有一个JSON对象作为String传递给我,但是String形式的Object包含重复的属性。我需要临时向属性添加递增数字,以避免重复JSON属性的问题。编辑完Object后,我将JSON.Stringify对象返回String并删除数字。

这是我传递的字符串:

{
    "View":{
        "Image":{
            "BackgroundImage":"Image.png",
             "Position":[0,0],
             "Width":320,
             "Height":480
        },
        "Button":{
            "BackgroundImage":"ButtonTop.png",
             "Position":[61,83],
             "Width":217,
             "Height":58
        },
        "Button":{
            "BackgroundImage":"ButtonBottom.png",
             "Position":[61,214],
             "Width":205,
             "Height":73
        },
        "TextField":{
            "BackgroundImage":"TextFieldLogin.png",
             "Position":[102,336],
             "Width":189,
             "Height":31
        },
        "Label":{
            "Position":[137,100],
             "Width":72,
             "Height":20,
             "Text":"Hi Steve",
             "FontSize":18,
             "Color":[0,0,0,1]
        },
        "Label":{
            "Position":[43,342],
             "Width":54,
             "Height":20,
             "Text":"Login:",
             "FontSize":18,
             "Color":[0,0,0,1]
        },
        "Label":{
            "Position":[115,234],
             "Width":54,
             "Height":20,
             "Text":"Button",
             "FontSize":18,
             "Color":[0,0,0,1]
        }
    }
}

以下是我希望输出的结果:

{
    "View_1":{
        "Image_1":{
            "BackgroundImage":"Image.png",
             "Position":[0,0],
             "Width":320,
             "Height":480
        },
        "Button_1":{
            "BackgroundImage":"ButtonTop.png",
             "Position":[61,83],
             "Width":217,
             "Height":58
        },
        "Button_2":{
            "BackgroundImage":"ButtonBottom.png",
             "Position":[61,214],
             "Width":205,
             "Height":73
        },
        "TextField_1":{
            "BackgroundImage":"TextFieldLogin.png",
             "Position":[102,336],
             "Width":189,
             "Height":31
        },
        "Label_1":{
            "Position":[137,100],
             "Width":72,
             "Height":20,
             "Text":"Hi Steve",
             "FontSize":18,
             "Color":[0,0,0,1]
        },
        "Label_2":{
            "Position":[43,342],
             "Width":54,
             "Height":20,
             "Text":"Login:",
             "FontSize":18,
             "Color":[0,0,0,1]
        },
        "Label_3":{
            "Position":[115,234],
             "Width":54,
             "Height":20,
             "Text":"Button",
             "FontSize":18,
             "Color":[0,0,0,1]
        }
    }
}

我如何使用javascript .replace()按需添加编号,然后根据需要删除编号?

2 个答案:

答案 0 :(得分:1)

您可以使用RegEx表达式并将函数传递给replace()方法以生成新名称。假设json包含字符串;

var i = 0;
json.replace(/\"Button\"/g, function(match) { return '"Button_' + i++ + '"'; });

有关详情,请参阅 Find&在本文Regular Expressions in JavaScript, part 2中替换为样式...

修改

要获取可能的对象名称数组,请使用此选项;

var names = json.match(/\"(\w*)\"\s?:\s?{/g)

然后,您可以循环遍历数组以执行所有替换;

for(n = 0; n < names.length; n++) {
   var i = 0;
   var name = names[n].replace(/\s?:\s?{/g,'');
   var re = new RegExp(name,'g');
   json = json.replace(re, function(match) { return '"' + name.replace(/"/g,'') + '_' + i++ + '"'; });
}

这次必须创建RegEx对象才能插入变量。

答案 1 :(得分:1)

这是怎么回事?我同意其他声音,建议任何提供此“JSON”的人都有责任提供有效的语法,但除非这种可能性,这可能会让你开始:

function formatJSON(input) {
  return input.replace(/"([^"]+?)":{(.+)}/g, function(string, key, value) {
    var dict = {};
    return '"' + key + '":{' + value.replace(/"([^"]+?)":{(.+?)}/g, function(string, key, value) {
      dict[key] = dict[key] == undefined ? 1 : ++dict[key];
      return '"' + key + '_' + dict[key] + '":{' + formatJSON(value) + '}';
    }) + '}';
  });
};

JSFiddle:http://jsfiddle.net/WYkAT/

请注意,这将无法重命名更复杂,更深入的JSON字符串上的所有键。它可以很容易地修改为更具递归性,并且不太适合您的特定情况,但可能会导致性能下降。如果您需要一个完全成熟的解决方案,我会寻求修改现有的JSON.parse polyfill。这是两个(JSON2和JSON3):

  1. https://github.com/douglascrockford/JSON-js
  2. https://github.com/bestiejs/json3