JSON对象:TypeError:element为null

时间:2017-09-27 18:02:37

标签: javascript jquery json ajax cors

我正在使用Ajax POST方法来获取跨域结果。结果正在返回,因为我执行:

var results = JSON.stringify(data);
console.log(results);

我得到以下内容:

{"ERR":null,"RSP":{"FLDR":{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505397375000","NM":"Documents","DESC":"Repository Root Folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":null,"FLDRS":[{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506442678000","NM":"Folder 1","DESC":"Level 1 Test Folder 1","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null},{"ID":"5","TJT":"FULL","ACTV":"true","BO":"1505397610000","LU":"1506442685000","NM":"Folder 4","DESC":"folder 4","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"8","TJT":"FULL","ACTV":"true","BO":"1506444461000","LU":"1506444461000","NM":"Test Folder","DESC":"Please delete this folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"5","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null},{"ID":"6","TJT":"FULL","ACTV":"true","BO":"1505405095000","LU":"1506442367000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"7","TJT":"FULL","ACTV":"true","BO":"1505405109000","LU":"1506442672000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"6","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null}],"DOCS":[{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505400744000","TTL":"Test Document 1","DESC":"Document 1","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":null,"VAL1":null,"VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":null,"VAL7":null,"VAL8":null,"VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"1","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null},{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506441814000","TTL":"Test Employee Document 1","DESC":"Document 2","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":"General Information","VAL1":"Pete Morgan","VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":"39.0","VAL7":null,"VAL8":"1505397375491","VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"2","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null}],"PROPS":null}}}

问题是当我执行此函数时,我收到以下错误:

  

TypeError:element为null

function getSubfolders(acctId,repoId,fldrId)
{
    $.ajax({
        type: 'POST',
        url: 'http://52.36.x.xxx/sdws/appv1/accts/' + storage.get('LoginID') + '/repos/' + repoId + '/fldrs/' + fldrId,
        crossDomain: true,
        xhrFields: {
            withCredentials: true
        },
        data: '{"CASCD":"true"}',
        dataType: "json",
        contentType: "application/json; charset=utf-8",

        success: function(data) {
            $.each(data.RSP.FLDR, function(index, element) {
                console.log(element.NM); 
            });         
        },
        error: function(e) {
            console.log(e);
        }           
    });
}

我不明白我在这里缺少什么。似乎每个语句都是循环的,但我得到的是“null”元素。基于我正在返回的对象,似乎我应该得到“文件夹1”,“文件夹4”等等。有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

您标记为“index”和“element”的变量实际上是data.RSP.FLDR中的键和值对 - 您的“null”结果是因为您尝试输出element.NM,当你想要的值实际上在element本身。

也就是说,您问题中的代码只搜索数据中的一个路径data.RSP.FLDR - 如果这就是您所需要的,那么您的代码可以是以下内容:

console.log(data.RSP.FLDR.NM)

如果您试图在数据树中找到所有NM值,则需要在树中进行递归搜索,而不是仅仅单个each循环。这有效,例如:

var data = {"ERR":null,"RSP":{"FLDR":{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505397375000","NM":"Documents","DESC":"Repository Root Folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":null,"FLDRS":[{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506442678000","NM":"Folder 1","DESC":"Level 1 Test Folder 1","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null},{"ID":"5","TJT":"FULL","ACTV":"true","BO":"1505397610000","LU":"1506442685000","NM":"Folder 4","DESC":"folder 4","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"8","TJT":"FULL","ACTV":"true","BO":"1506444461000","LU":"1506444461000","NM":"Test Folder","DESC":"Please delete this folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"5","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null},{"ID":"6","TJT":"FULL","ACTV":"true","BO":"1505405095000","LU":"1506442367000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"7","TJT":"FULL","ACTV":"true","BO":"1505405109000","LU":"1506442672000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"6","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null}],"DOCS":[{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505400744000","TTL":"Test Document 1","DESC":"Document 1","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":null,"VAL1":null,"VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":null,"VAL7":null,"VAL8":null,"VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"1","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null},{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506441814000","TTL":"Test Employee Document 1","DESC":"Document 2","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":"General Information","VAL1":"Pete Morgan","VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":"39.0","VAL7":null,"VAL8":"1505397375491","VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"2","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null}],"PROPS":null}}}

var searchTree = function(obj) {
  if (obj && typeof(obj) === 'object') {
    $.each(obj, function(k, v) {
      if (k === 'NM') {
        console.log(v) // found one
      }
      searchTree(v); // recurse to child objects 
    })
  }
}

searchTree(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>