我得到一个传递的字符串,它是一个独特的格式(虽然是一致的),我需要能够解析它,以便我可以修改它的各个部分,然后将它放回一起并将其作为字符串传回。
这是我通过的字符串。它的内容会定期更改,但每个项目的结构将保持不变。
View
{
Name: View1;
Image
{
BackgroundImage: Image.gif;
Position: 0, 0;
Width: 320;
Height: 480;
}
Button
{
BackgroundImage: Button.gif;
Transition: View2;
Position: 49, 80;
Width: 216;
Height: 71;
}
Button
{
BackgroundImage: Button2.gif;
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;
}
Scroll
{
Position: 106, 91;
Width: 96;
Height: 34;
Button{
BackgroundImage: Button2.gif;
Position: 65, 217;
Width: 188;
Height: 134;
}
Button{
BackgroundImage: Button2.gif;
Position: 65, 217;
Width: 188;
Height: 134;
}
}
}
我想我需要一个递归函数来查找每个 k,v 并将其放入适当的 Object 或 JSON 文件中,以便我可以修改。我所能得到的最远的是能够解析单个级别并将其放入 k,v 对象中。这是它解析的代码和字符串。
修改字符串以使用我的代码(单级深度。我删除了 View{} 和 Scroll{}):
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: 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;
}
var initObjectList = '<div id="prePop">';
$.each(obj.Controls, function (k, v) {
initObjectList += '<div class="inLineObjects">' + '<div class="key">' + k + '</div><br/>' + '<div class="value">' + v + '</div>' +'</div>';
});
initObjectList += '</div>';
$('#code').append(initObjectList)
回报:
{
"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;}"
}
}
我的问题是上面不允许我a)。定位 {} 之外的任何内容,因为它都是一个值和 b)。它没有任何类型的递归能力来处理多个级别,即 View>button>scrollview
对此的任何帮助将不胜感激!