【问题标题】:Javascript load Array from Text FileJavascript从文本文件加载数组
【发布时间】:2020-07-24 20:26:16
【问题描述】:

所以快速解释我是否将myObj from = array 更改为myObj = { the content of the callback.txt file } 然后这行得通。但是,当我尝试将内容调用到 var 中时,当我将数组调用到 innerHTML 时却没有看到页面上的全部内容。所以我知道文件正在正确加载,但是我无法让它解析文件以获取我想要的信息或将其作为数组运行。

数组文件为

{ "information": [
  { 
    "Telephone # Dialed": "8555551234",
    "Employee ID": "XYZ456",
    "IncidentID": "INC000022222226",
    "Domain": "CORP",
    "Tier": "903",
    "HierarchyCode": "HACA4564S",
    "First Name": "Jane",
    "Last Name": "Smith",
    "City": "NORTH LAS VEGAS",
    "State": "NV",
    "Office Phone": "1234",
    "CallbackNumber": "4567",
    "Callback Successful": "N/A",
    "Callback Attempts": "0"
  },
  {
    "Telephone # Dialed": "8555551234",
   "Employee ID": "XYZ456",
    "IncidentID": "INC000022222228",
    "Domain": "CORP",
   "Tier": "903",
   "HierarchyCode": "HACA4564S",
   "First Name": "John",
    "Last Name": "Smith",
    "City": "NORTH LAS VEGAS",
    "State": "NV",
    "Office Phone": "555",
    "CallbackNumber": "1234",
    "Callback Successful": "N/A",
    "Callback Attempts": "0"
  }
]
}

和 JavaScript 一样

var array = [];
var xmlhttp;
if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
} else {
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var text = xmlhttp.responseText;
    array = text.split(/\n|\r/g);
    var obj = JSON.parse(text);
    array = obj.information;
    var listener = document.getElementById("submitThis");


    listener.onclick = function() {

      var incident = document.getElementById("incident").value;
      var myObj, i, j, x = "";
      myObj = array;
      for (i in myObj.information) {
        if (myObj.information[i].IncidentID == incident) {
          for (j in myObj.information[i].CallbackNumber) {
            x = myObj.information[i].CallbackNumber;
          }
        }
      }
      document.getElementById("test").innerHTML = x;

    }
  }
}


xmlhttp.open("GET", "callback.txt", true);
xmlhttp.send();
<html>

<body>
  <p id="test"></p>
  <input type="text" id="incident">
  <input type="button" id="submitThis" value="make it happen">
</body>

</html>

【问题讨论】:

    标签: javascript html arrays json


    【解决方案1】:

    您的问题是您试图访问数组中不存在的对象(您已经在前面的代码中调用了该对象)。

    <html>
    <body>
    
    
    <p id="test"></p>
    <input type="text" id="incident"> 
    <input type="button" id="submitThis"  value="make it happen">
    <script>
    var array = [];
    var xmlhttp;
    if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
    } else {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var text = xmlhttp.responseText;
    //array = text.split(/\n|\r/g);
    var obj = JSON.parse(text); 
    array = obj.information;
    var listener = document.getElementById("submitThis");
    
    
    listener.onclick = function() { 
    
    var incident = document.getElementById("incident").value;
    var myObj, i, j, x = "";
    myObj = array ;
    for (i in myObj) {
    if ( myObj[i].IncidentID == incident ) {
      for (j in myObj[i].CallbackNumber) {
       x = myObj[i].CallbackNumber ;
      }
      }
    }
    document.getElementById("test").innerHTML = x;
    
    }
      }
    }
    
    
    xmlhttp.open("GET", "callback.txt", true);
    xmlhttp.send();
    </script>
    
    </body>
    </html>
    

    array = obj.information;,您已经访问了信息。然后你尝试在你的 for 循环和 for 循环内的代码中再次获取它(即:for (i in myObj.information) {)。

    我还注释掉了array = text.split(/\n|\r/g);,因为它在您的代码中没有做任何事情。无论如何,您都不想在运行 JSON.parse 之前拆分 JSON 输入。

    我希望这有助于澄清问题。

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多