2011-06-23 67 views
0

讀說,我已經保存在一個文件subsysteminformation.txt信息成陣列

#{ 
    "buildnames": { 
     label: "buildnames", 
     data: ["test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4", 
       "test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4","test2-a1", "test2-a2", "test2-a3", "test2-a4/" 
     ] 
     }}; 


#{ 
    subsystem: "CAS", 
    date: { 
     label: "CAS", 
     data: [null] 
    },   
    time: { 
     label: "CAS", 
     data: [null] 
    }, 
    modules: { 
     label: "CAS", 
     data: [null] 
    }, 
    cases: { 
     label: "CAS", 
     data: [null] 
    }, 
    failed: { 
     label: "CAS", 
        data: [[0, 239], [1, 249], [2, 237], [3, 192],[4, 234], [5, 288], [6, 234], [7, 195],[8, 245], [9, 181], [10, 294], [11, 195],[12, 249], [13, 235], [14, 262], [15, 155], 
      [16, 245], [17, 121], [18, 372], [19, 195],[20, 255], [21, 231], [22, 332], [23, 195],[24, 225], [25, 271], [26, 272], [27, 195],[28, 255], [29, 131], [30, 332], [31, 165], 
      [32, 245], [33, 201], [34, 132], [35, 195],[36, 245], [37, 211], [38, 232], [39, 195],[40, 245], [41, 231], [42, 262], [43, 195],[44, 245], [45, 261], [46, 252], [47, 155], 
      [48, 245], [49, 161], [50, 172], [51, 195],[52, 245], [53, 241], [54, 212], [55, 195],[56, 245], [57, 211], [58, 232], [59, 195],[60, 245], [61, 201], [62, 232], [63, 155], 
      [64, 245], [65, 231], [66, 232], [67, 195],[68, 245], [69, 211], [70, 232], [71, 195],[72, 245], [73, 231], [74, 232], [75, 195],[76, 245], [77, 231], [78, 232], [79, 145], 
      [80, 245], [81, 231], [82, 232], [83, 195],[84, 245], [85, 231], [86, 232], [87, 195],[88, 245], [89, 171], [90, 132], [91, 195],[92, 245], [93, 231], [94, 232], [95, 135], 
      [96, 245], [97, 181], [98, 302], [99, 195],[100, 245], [101, 214], [102, 252], [103, 195],[104, 255], [105, 261], [106, 262], [107, 195],[108, 205], [109, 311], [110, 232], [111, 135], 
      [112, 245], [113, 231], [114, 242], [115, 195],[116, 245], [117, 231], [118, 232], [119, 195],[120, 245], [121, 211], [122, 223], [123, 195],[124, 235], [125, 231], [126, 312], [127, 135] 
      ] 
    }, 
    cover: { 
     label: "CAS", 
     data: [[0, 235], [1, 239], [2, 237], [3, 192],[4, 234], [5, 288], [6, 234], [7, 195],[8, 245], [9, 181], [10, 294], [11, 195],[12, 249], [13, 235], [14, 262], [15, 155], 
      [16, 245], [17, 121], [18, 372], [19, 195],[20, 255], [21, 231], [22, 332], [23, 195],[24, 225], [25, 271], [26, 272], [27, 195],[28, 255], [29, 131], [30, 332], [31, 165], 
      [32, 245], [33, 201], [34, 132], [35, 195],[36, 245], [37, 211], [38, 232], [39, 195],[40, 245], [41, 231], [42, 262], [43, 195],[44, 245], [45, 261], [46, 252], [47, 155], 
      [48, 245], [49, 161], [50, 172], [51, 195],[52, 245], [53, 241], [54, 212], [55, 195],[56, 245], [57, 211], [58, 232], [59, 195],[60, 245], [61, 201], [62, 232], [63, 155], 
      [64, 245], [65, 231], [66, 232], [67, 195],[68, 245], [69, 211], [70, 232], [71, 195],[72, 245], [73, 231], [74, 232], [75, 195],[76, 245], [77, 231], [78, 232], [79, 145], 
      [80, 245], [81, 231], [82, 232], [83, 195],[84, 245], [85, 231], [86, 232], [87, 195],[88, 245], [89, 171], [90, 132], [91, 195],[92, 245], [93, 231], [94, 232], [95, 135], 
      [96, 245], [97, 181], [98, 302], [99, 195],[100, 245], [101, 234], [102, 232], [103, 195],[104, 245], [105, 231], [106, 232], [107, 195],[108, 245], [109, 301], [110, 232], [111, 135], 
      [112, 245], [113, 231], [114, 242], [115, 195],[116, 245], [117, 231], [118, 232], [119, 195],[120, 245], [121, 211], [122, 223], [123, 195],[124, 235], [125, 231], [126, 312], [127, 135] 
      ] 
    }}; 

而下面的腳本中的信息,請閱讀以下信息。

var xmlhttp; 
function loadXMLDoc(url,cfunc){ 
    if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } 
    else 
     {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    xmlhttp.onreadystatechange=cfunc; 
    xmlhttp.open("GET",url,true); 
    xmlhttp.send(); 
} 


function myFunction(){ 
    loadXMLDoc("subsysteminformation.txt",handleXML); 
} 
var checkState = function(xmlhttp, callback) { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
    callback(); 
    } else { 
    // Check back again 1 sec later 
    setTimeout(checkState, 1000); 
    } 
}; 


function handleXML() 
    { 
checkState(xmlhttp, function() { 
    var txt=xmlhttp.responseText; 
    var datasets = []; 
    var datasetsCounter =0; 
    var secondPos; 
    var aPosition; 
    var currentCharacterLocation; 
while(txt.indexOf("#") > -1){ 
    aPosition = txt.indexOf("#"); 
    secondPos = txt.indexOf("#", (aPosition)+1); 
    aPosition++; 
    datasets[datasetsCounter]=txt.substring(aPosition, secondPos); 
    //var stringToCompare = txt.substring(aPosition, secondPos); 
    txt = txt.substring(secondPos); 
    datasetsCounter++; 
    document.write("done"); 

}

}); 
    } 
</script> 
</head> 
<body onLoad="myFunction()"> 
</body> 
</html> 

現在我的問題

這部分是極其錯誤的

而(txt.indexOf( 「#」)> - 1){
aPosition = txt.indexOf(「#」); secondPos = txt.indexOf(「#」,(aPosition)+1); aPosition ++; 數據集[datasetsCounter] = txt.substring(aPosition,secondPos); // var stringToCompare = txt.substring(aPosition,secondPos); txt = txt.substring(secondPos); datasetsCounter ++; document.write(「done」); }

我想要做的是檢查文本文件中是否有更多的「#」字符,如果是這樣讀取所有內容(直到下一個「#」字符或直到沒有更多的字符在所有在文檔中)到陣列數據集[我],但我從來沒有通過第一個#,也許你們中有些聰明的人可以幫助我做我做錯了什麼,我應該寫什麼,而不是「如果txt.indexOf(「#」)「來檢查subsysteminformation.txt文件中是否還有」#「字符?

另外,如果沒有更多的」#「符號,我該如何讀取最後一位信息?字符在文字中?

謝謝事先=)

編輯I'ts一個加號,如果該sollution快,因爲我會在文件subsysteminformation.txt大約100 「#」 字符=)

EDIT 2的問題是,我從來沒有走出「While」循環,它只是繼續前進,我只是希望一旦使用了一個「#」字符,就移動到下一個,不要重複使用相同的「#」 「性格再次。 =)

+2

其他人如果不縮小範圍,可能會將您的問題視爲「過於本地化」,這樣人們就可以知道提問的內容,主要問題是什麼等等。我敢打賭,不需要這些代碼的大部分,並且它的瀏覽時間太長。 – ninjagecko

+0

@ninjagecko問題是,我從來沒有離開「while」循環,它只是繼續前進 – Adam

回答

0

,你可以有這樣的事情

var數據= xmlhttp.responseText.match(/^#(.[^;]*)/克).MAP(函數(項目){返回的eval( 「(」 + item.replace(/ ^#/, '')+ 「)」)})

那麼數據集將被評估對象的數組從文本文件

+0

@TheBrain對不起,你可以請再次快速看一下這條線,它表示它期待另一個') ',當我像這樣添加它時 var datasets = xmlhttp.responseText.match(/^#(.[^#]*)/ gm).map(function(item){return eval(「(」 + item.replace(/ ^#/,'')+「)」})); 它仍然抱怨(期待另一個')',我在做什麼錯了?我知道這是一個愚蠢的問題:P – Adam

+0

是的,對不起var dataset = xmlhttp.responseText.match(/^#(.[^#]*)/ gm).map(function(item){return eval(「(」 + item.replace(/ ^#/,'')+「)」)}) – TheBrain

+0

@Adam,您可能需要將match()規則更改爲/^#(.[^;]*)/否則,您會有eval(「({name:value};)」),它會拋出一個錯誤。如果你讀到的所有內容都是';'那麼它應該工作得很好。 – TheBrain