在這段代碼中,我帶入一個沒有結構化的字符串,並將其解析爲一個JS對象。我現在需要將其應用於Web表單。複雜的JS對象填充可點擊的列表和表格
Here is the demo。它顯示Parsed/Structured對象層次結構並警告一個目標項目的示例。
創建關鍵項目(即按鈕,圖像,標籤等
點擊列表項的可點擊列表,填充形式與價值
修改的值並保存回對象
有一些注意事項:
- 有重複鍵。我不知道如何讓他們獨一無二,同時保持數據的完整性。我無法嵌套它們。我唯一能想到的就是給它們添加一個唯一的ID
- 理想情況下,表單將根據對象中的字段動態生成字段。 (即「按鈕」具有寬度,高度,過渡和名稱,其中'標籤'具有不同的字段集合
- 某些元素是嵌套的(即「滾動」有它自己的'按鈕'和'標籤「
任何幫助,將不勝感激。下面的代碼並創建元素的列表,但我不能讓它根據我點擊值加載到一種形式。
這裏是代碼
//Parse String
var str = 'View\n{\n Name: View1;\n Image\n {\n BackgroundImage: Image.gif;\n Position: 0, 0;\n Width: 320;\n Height: 480;\n }\n\n Button\n {\n BackgroundImage: Button.gif;\n Transition: View2;\n Position: 49, 80;\n Width: 216;\n Height: 71;\n }\n\n Button\n {\n BackgroundImage: Button2.gif;\n Position: 65, 217;\n Width: 188;\n Height: 134;\n }\n\n Label\n {\n Position: 106, 91;\n Width: 96;\n Height: 34;\n Text: "Button";\n FontSize: 32;\n Color: 0.12549, 0.298039, 0.364706, 1;\n }\n Scroll\n {\n Position: 106, 91;\n Width: 96;\n Height: 34;\n Button{\n BackgroundImage: Button2.gif;\n Position: 65, 217;\n Width: 188;\n Height: 134;\n }\n Button{\n BackgroundImage: Button2.gif;\n Position: 65, 217;\n Width: 188;\n \n Height: 134;\n }\n\n }\n\n}';
str = str.replace(/(\w+)\s*\{/g, "$1:{"); // add in colon after each named object
str = str.replace(/\}(\s*\w)/g, "},$1"); // add comma before each new named object
str = str.replace(/;/g, ","); // swap out semicolons with commas
str = str.replace(/,(\s+\})/g, "$1"); // get rid of trailing commas
str = str.replace(/([\d\.]+(, [\d\.]+)+)/g, "[$1]"); // create number arrays
str = str.replace(/"/g, ""); // get rid of all double quotes
str = str.replace(/:\s+([^\[\d\{][^,]+)/g, ':"$1"'); // create strings
$("#parseList").html(str);
var objStr;
eval("objStr={" + str + "};");
//console.log(objStr.View.Button)
//alert(objStr.View.Scroll.Button.Width);
//End Parse String
$(document).ready(function() {
//Build Initial Object LIst
var initObjectList = '<div id="prePop">';
$.each(objStr.View, function (k, v) {
initObjectList += '<div class="inLineObjects">' + '<div class="key">' + k + '</div>' + '</div>';
});
initObjectList += '</div>';
$('#code').append(initObjectList)
對象輸出示例:
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
}
}
}
哎呀,使用'JSON'對象的'parse'方法來解析的是,沒有必要用你自己的工作了正則表達式。 – Austin
這裏真正的問題是什麼?一般來說,如果您儘可能具體地提出問題,而不是包含大量內容並說「任何幫助將不勝感激」,您將在Stack Overflow上得到更好的響應。我們喜歡在這裏給予幫助,你不需要問;-)但是你確實需要(或者至少你會得到更好的答案,如果你的話)讓我們儘可能簡單地幫助你。 – machineghost
你無法控制這個輸出嗎?如果服務器將向您發送json,則需要以正確的格式向您發送_valid_ json(重複鍵在JSON規範中無效)。或者,也許我在這裏錯過了一些東西,是什麼樣的特定格式(CSS)的輸入? – Austin