从JSON数组生成多维数组时出错

时间:2015-04-14 16:18:33

标签: javascript

我使用JavaScript通过PHP从服务器获取数据。

PHP从MySQL获取数据,将其编码为JSON数据并通过HTTP协议将其发送到客户端 - 在我的情况下,即浏览器。

JavaScript,如下所示,选择这些值并呈现图表。仅用于测试我打印出JSONArray转换为StringArray,这是PHP的输出。

关于为何我的JS代码到达下面的原因的任何建议

myLogger("myLogger - newObject.dummmysetsJSONArr.entryID" + newObject.dummmysetsJSONArr.entryID);

它会在控制台中显示以下错误:

   uncaught typeerror cannot read property 'entryID' of undefined

以下是转换为JSON的数据库示例:

{"dummmysetsJSONArr":[{"entryID":"1","distance":"100","calories":"50"},{"entryID":"2","distance":"200","calories":"100"},{"entryID":"3","distance":"300","calories":"150"},{"entryID":"4","distance":"400","calories":"200"},{"entryID":"5","distance":"500","calories":"250"},{"entryID":"6","distance":"600","calories":"300"}],"success":1}

这是我的JS:

    google.setOnLoadCallback(drawVisualization);

    function drawVisualization() {

      var req = false;
      var jsonarry;

      try {
          // most browsers
          req = new XMLHttpRequest();

          myLogger("myLogger - XMLHttpRequest() created");
      } catch (e){
          // IE
          try{
              req = new ActiveXObject("Msxml2.XMLHTTP");

              myLogger("myLogger - req = new ActiveXObject(Msxml2.XMLHTTP);");          
          } catch (e) {
              // try an older version
              try{
                  req = new ActiveXObject("Microsoft.XMLHTTP");

                  myLogger("myLogger - req = new ActiveXObject(Microsoft.XMLHTTP);");               
              } catch (e){

              }
          }
      }

  if (!req) { 
    myLogger("req === false");
  } else {

    myLogger("req === true");
  }

  // Use onreadystatechange property
  req.onreadystatechange = function() {

       //myLogger("myLogger - req.onreadystatechange = function(){");

      if(req.readyState == 4) {

          myLogger("myLogger - req.readyState == 4");

          if(req.status === 200) {

            myLogger("myLogger - req.status === 200");

            jsonarry = req.responseText;

            myLogger("myLogger - JSON ARRAY - " + jsonarry);

            myLogger(" ------------- ");

            var identedText = JSON.stringify(jsonarry, null, 4);

            var jsonString = JSON.stringify(jsonarry);

            myLogger("myLogger - Unindented jsonString - " + jsonString);

            var newObject = JSON.parse(jsonString);

            myLogger("myLogger - newObject " + newObject);

            myLogger("myLogger - newObject.dummmysetsJSONArr.entryID" + newObject.dummmysetsJSONArr.entryID); 
}

function myLogger(content) {
    if (window.console && window.console.log) {
        console.log("myLogger - " + content);
    }
}

编辑:我通过删除不相关的位来减少我的OP代码。

赞赏任何想法

1 个答案:

答案 0 :(得分:2)

错误在这一行

myLogger("myLogger - newObject.dummmysetsJSONArr.entryID" + newObject.dummmysetsJSONArr.entryID);

当您尝试记录newObject.dummmysetsJSONArr.entryID时。

实际上,newObject.dummmysetsJSONArr.entryID未定义,因为newObject.dummmysetsJSONArr是一个数组。

例如,newObject.dummmysetsJSONArr[0].entryID有效。