2

文字列を配列に分解しようとしています。以下のこのコードはそれを行いますが、今度はそれをいずれかのサブ配列に分解する必要があります。

文字列とコードは次のとおりです。

$(document).ready(function() {
var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}';

var result = content.split('}');
result.pop();// removing the last empty element
console.log(result);
for(var i=0;i<result.length;i++)
{
    result[i]+='}';
    console.log(result);
        $('div').append('<li>' + result[i] + '</li>');
}
})

これは次のようになります。

<li>Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}</li>
<li>Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}</li>
<li>Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}</li>
<li>Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}</li>

私が今やらなければならないことは、それをさらに分解して、{}の前に単語があるようにすることです。つまり、最初の画像の画像です。

理想的には、出力をこのようなキー/値オブジェクトにしたいと思います

    {
        "Controls": [{ "Image":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
    "Button":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
"Button":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
    "Label":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", }],
    }

私の主な目標は、この最後にキーまたは値のいずれかをターゲットにできるようにすることです。

どんな助けでも大歓迎です

4

3 に答える 3

0

正規表現を使用する方が良いかもしれません

// the regexp
var re = /(.+?)(\{.*?\})/g,
    group;

var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}';

var ret = {};

while(group = re.exec(content)) {
    var k = group[1],
        v = group[2];

    ret[k] = ret[k] ? ret[k].substring(0, ret[k].length - 1) + v.substring(1, v.length) : v;
}


// output
console.log(ret);   // => {'Button': '{xxx}', 'Image': '{xxxx}', 'Label': '{xxxx}'}
于 2012-12-27T03:32:45.960 に答える
0

これにより、必要な出力が生成されます。

$(document).ready(function() {
    var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}';

    var result = content.split('}');
    result.pop();// removing the last empty element
    var obj = {Controls:{}};

    $.each(result, function (i, v) {
        var key = v.split('{');
        var value = v.replace(key[0], '') + '}';

        if (key[0] !== 'Button') {
            obj.Controls[key[0]] = value;
        } else {
            if (!obj.Controls.hasOwnProperty('Buttons')) {
                obj.Controls['Buttons'] = [];
            }   
            obj.Controls.Buttons.push(value);
        }
    });

    console.log(obj);
});​

作業デモ: http: //jsfiddle.net/fewds/TuNTV/2/

出力例:

{
    "Controls": {
        "Image": "{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
        "Buttons": [
            "{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}",
            "{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}"
        ],
        "Label": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}"
    }
}

複数のキーオカレンスをインクリメントしたいだけの場合は、次を使用できます。

$(document).ready(function() {
    var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Image{BackgroundImage: Image2.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Button{BackgroundImage: Button3.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}';

    var result = content.split('}');
    result.pop();// removing the last empty element
    var obj = {Controls:{}};

    function nextProp(key) {
        if(obj.Controls.hasOwnProperty(key)) {
            var num = key.match(/\d+$/);
            if (num) {
                return nextProp(key.replace(num[0], '') + (parseInt(num[0], 10) + 1));
            } else {
                return nextProp(key + '1');
            }
        }

        return key;
    }

    for (var i = 0; i < result.length; i++) {
        var key = result[i].split('{');
        var value = result[i].replace(key[0], '') + '}';
        obj.Controls[nextProp(key[0])] = value;
    }

    console.log(obj);
});​

作業デモ: http: //jsfiddle.net/fewds/TuNTV/5/

出力例:

{
    "Controls": {
        "Image": "{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
        "Image1": "{BackgroundImage: Image2.gif;Position: 0, 0;Width: 320;Height: 480;}",
        "Button": "{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}",
        "Button1": "{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}",
        "Button2": "{BackgroundImage: Button3.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}",
        "Label": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}",
        "Label1": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}"
    }
}
于 2012-12-27T03:49:14.393 に答える
0

これを試して。キー値をターゲットにする場合は、JSONに解析できます。しかし、これは有効なJSONではないと思います。

var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}' +
'Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}' +
'Button2{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}' +
'Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}';

// add colons after the keys
content = content.replace(/([\w]+){/g, '"$1":{');

// wrap all {} in quotes
content = content.replace(/({[^}]+})/g, '"$1",');

// remove trailing comma
content = content.slice(0, -1);

content = '{"Controls": [{' + content + '}]}';

console.log(content);
于 2012-12-27T03:49:42.937 に答える