2011-06-23 66 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