【问题标题】:Why is my JSON array response returns undefined when I try to access specific value为什么当我尝试访问特定值时我的 JSON 数组响应返回未定义
【发布时间】:2020-05-17 15:25:36
【问题描述】:

我想从 AJAX 响应返回的嵌套数组中获取特定值, 下面是我来自 jsp 的 ajax 调用,并在下面发布了返回响应。

  $.ajax({

        url: "servletCall.jsp",
        type: "POST",
        dataType:"html",
        data: {
        appId:appID,    
        functionalID : functionalID,
         async:false,
            cache: false,
        },
        ContentType:'json',
        success: function(data){

            data = data.replace(/\\n/g, "\\n")  
               .replace(/\\'/g, "\\'")
               .replace(/\\"/g, '\\"')
               .replace(/\\&/g, "\\&")
               .replace(/\\r/g, "\\r")
               .replace(/\\t/g, "\\t")
               .replace(/\\b/g, "\\b")
               .replace(/\\f/g, "\\f");

data = data.replace(/[\u0000-\u001F]+/g,"");  

 var jsonObj = JSON.parse(JSON.stringify(data));

// console.log('NOTHINH'+jsonObj) ;
  newString = jsonObj.replace(/<(?:.|\n)*?>/gm, '');

   // console.log(newString) ;
 alert(newString['functionalSubDomainName']);
 alert(newString['jsonArray2']);
 alert(newString.functionalSubDomainName);

)};

newString:JSON 响应:来自 ServletCall.jsp,后来被 stringfy 解析

{
 "jsonArray2":[
  {
     "pkApplicationid":450.0,
     "success":false,
     "numberOfCustomerUsers":0,
     "numberofCustomers":0,
     "dGFFCorpSecInfo":"",
     "dGFFCorpSecDisas":"",
     "securitycompliancecomment":"",
     "dedicatedFTE":0,
     "noOfCountries":0,
     "noOfCountriesR":0,
     "noOfA2AInterfaces":0,
     "noOfA2AInterfacesR":0,
     "noOfB2BInterfaces":0,
     "noOfB2BInterfacesR":0,
     "totalNoOfUsers":0,
     "totalNoOfUsersR":0,
     "noOfStations":0,
     "productWrite":false,
     "functionalDomainID":1018,
     "functionalDomainName":"Product Operations / Customer Service",
     "functionalSubDomainID":1020,
     "functionalSubDomainName":"VAS-WMS"
  },     
  {
     "pkApplicationid":450.0,
     "success":false,
     "numberOfCustomerUsers":0,
     "numberofCustomers":0,
     "dGFFCorpSecInfo":"",
     "dGFFCorpSecDisas":"",
     "securitycompliancecomment":"",
     "dedicatedFTE":0,
     "noOfCountries":0,
     "noOfCountriesR":0,
     "noOfA2AInterfaces":0,
     "noOfA2AInterfacesR":0,
     "noOfB2BInterfaces":0,
     "noOfB2BInterfacesR":0,
     "totalNoOfUsers":0,
     "totalNoOfUsersR":0,
     "noOfStations":0,
     "productWrite":false,
     "functionalDomainID":1021,
     "functionalDomainName":"Finance",
     "functionalSubDomainID":1049,
     "functionalSubDomainName":"Enterprise Performance Management"
  },      
  {
     "pkApplicationid":450.0,
     "success":false,
     "numberOfCustomerUsers":0,
     "numberofCustomers":0,
     "dGFFCorpSecInfo":"",
     "dGFFCorpSecDisas":"",
     "securitycompliancecomment":"",
     "dedicatedFTE":0,
     "noOfCountries":0,
     "noOfCountriesR":0,
     "noOfA2AInterfaces":0,
     "noOfA2AInterfacesR":0,
     "noOfB2BInterfaces":0,
     "noOfB2BInterfacesR":0,
     "totalNoOfUsers":0,
     "totalNoOfUsersR":0,
     "noOfStations":0,
     "productWrite":false,
     "functionalDomainID":1590,
     "functionalDomainName":"Supporting Applications",
     "functionalSubDomainID":1064,
     "functionalSubDomainName":"Document Logistics"
  },      
  {
     "pkApplicationid":450.0,
     "success":false,
     "numberOfCustomerUsers":0,
     "numberofCustomers":0,
     "dGFFCorpSecInfo":"",
     "dGFFCorpSecDisas":"",
     "securitycompliancecomment":"",
     "dedicatedFTE":0,
     "noOfCountries":0,
     "noOfCountriesR":0,
     "noOfA2AInterfaces":0,
     "noOfA2AInterfacesR":0,
     "noOfB2BInterfaces":0,
     "noOfB2BInterfacesR":0,
     "totalNoOfUsers":0,
     "totalNoOfUsersR":0,
     "noOfStations":0,
     "productWrite":false,
     "functionalDomainID":1590,
     "functionalDomainName":"Supporting Applications",
     "functionalSubDomainID":1392,
     "functionalSubDomainName":"Activity Monitoring and Business Rules"
  }
]
}

我的所有警报都返回未定义。 请帮我解决。

【问题讨论】:

  • 您的 success: function(data){...} 应该已经返回了一个 JavaScript 对象,而您无需进行任何额外的处理 - 因此您应该能够删除所有这些 replace() 转换。然后,您可以像这样访问数据数组:var myArray = data.jsonArray2;,或者像这样:jsonObj["jsonArray2"]。然后你可以用普通的 JavaScript 方式遍历数组,以访问数组中每个对象的详细信息。
  • 另外,您已将此问题标记为java 问题 - 这是不正确的。您应该改用javascript 标签。
  • @andrewjames:它返回一个 JS 对象,我可以在 console.log 中打印 JSON 对象。
  • 如果我使用这个 for(var i in newString){ console.log(newString [i]);},我会逐字垂直打印所有值
  • 我建议您编辑您的问题,显示您的(更新的)代码,并显示相关的输出。当我使用以下内容时:console.log(myArray[2].functionalSubDomainName);,我得到一个包含“文档物流”的普通字符串 - 都在一行上。

标签: javascript json ajax jsp


【解决方案1】:

您来自服务器的数据已经是 json,您不需要再次parse。此外,您可以使用 response.jsonArray2[i].yourkeyname 从您的 json 响应中获取任何数据。

演示代码

//your response
var response = {
  "jsonArray2": [{
      "pkApplicationid": 450.0,
      "success": false,
      "numberOfCustomerUsers": 0,
      "numberofCustomers": 0,
      "dGFFCorpSecInfo": "",
      "dGFFCorpSecDisas": "",
      "securitycompliancecomment": "",
      "dedicatedFTE": 0,
      "noOfCountries": 0,
      "noOfCountriesR": 0,
      "noOfA2AInterfaces": 0,
      "noOfA2AInterfacesR": 0,
      "noOfB2BInterfaces": 0,
      "noOfB2BInterfacesR": 0,
      "totalNoOfUsers": 0,
      "totalNoOfUsersR": 0,
      "noOfStations": 0,
      "productWrite": false,
      "functionalDomainID": 1018,
      "functionalDomainName": "Product Operations / Customer Service",
      "functionalSubDomainID": 1020,
      "functionalSubDomainName": "VAS-WMS"
    },
    {
      "pkApplicationid": 450.0,
      "success": false,
      "numberOfCustomerUsers": 0,
      "numberofCustomers": 0,
      "dGFFCorpSecInfo": "",
      "dGFFCorpSecDisas": "",
      "securitycompliancecomment": "",
      "dedicatedFTE": 0,
      "noOfCountries": 0,
      "noOfCountriesR": 0,
      "noOfA2AInterfaces": 0,
      "noOfA2AInterfacesR": 0,
      "noOfB2BInterfaces": 0,
      "noOfB2BInterfacesR": 0,
      "totalNoOfUsers": 0,
      "totalNoOfUsersR": 0,
      "noOfStations": 0,
      "productWrite": false,
      "functionalDomainID": 1021,
      "functionalDomainName": "Finance",
      "functionalSubDomainID": 1049,
      "functionalSubDomainName": "Enterprise Performance Management"
    },
    {
      "pkApplicationid": 450.0,
      "success": false,
      "numberOfCustomerUsers": 0,
      "numberofCustomers": 0,
      "dGFFCorpSecInfo": "",
      "dGFFCorpSecDisas": "",
      "securitycompliancecomment": "",
      "dedicatedFTE": 0,
      "noOfCountries": 0,
      "noOfCountriesR": 0,
      "noOfA2AInterfaces": 0,
      "noOfA2AInterfacesR": 0,
      "noOfB2BInterfaces": 0,
      "noOfB2BInterfacesR": 0,
      "totalNoOfUsers": 0,
      "totalNoOfUsersR": 0,
      "noOfStations": 0,
      "productWrite": false,
      "functionalDomainID": 1590,
      "functionalDomainName": "Supporting Applications",
      "functionalSubDomainID": 1064,
      "functionalSubDomainName": "Document Logistics"
    },
    {
      "pkApplicationid": 450.0,
      "success": false,
      "numberOfCustomerUsers": 0,
      "numberofCustomers": 0,
      "dGFFCorpSecInfo": "",
      "dGFFCorpSecDisas": "",
      "securitycompliancecomment": "",
      "dedicatedFTE": 0,
      "noOfCountries": 0,
      "noOfCountriesR": 0,
      "noOfA2AInterfaces": 0,
      "noOfA2AInterfacesR": 0,
      "noOfB2BInterfaces": 0,
      "noOfB2BInterfacesR": 0,
      "totalNoOfUsers": 0,
      "totalNoOfUsersR": 0,
      "noOfStations": 0,
      "productWrite": false,
      "functionalDomainID": 1590,
      "functionalDomainName": "Supporting Applications",
      "functionalSubDomainID": 1392,
      "functionalSubDomainName": "Activity Monitoring and Business Rules"
    }
  ]
};
var new_data = "";
//looping through jsonArray2
for (var i = 0; i < response.jsonArray2.length; i++) {
  //do same for other values also 
  new_data += "pkApplicationid : " + response.jsonArray2[i].pkApplicationid + "  || functionalSubDomainName : " + response.jsonArray2[i].functionalSubDomainName + "<br><br>";
}
//append result to some div
$(".data").html(new_data);

var new_data1 = "<table border='1'><tr><th>Supporting Applications</th></tr>";

for (var i = 0; i < response.jsonArray2.length; i++) {
  //checking against the functionalDomainName if same
  if (response.jsonArray2[i].functionalDomainName === "Supporting Applications") {
    new_data1 += "<tr><td> " + response.jsonArray2[i].functionalSubDomainName + "</td></tr>";
  }
}
name_data1 = "</table>"
//append result to some div
$(".data2").html(new_data1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
</div>
<div class="data2">
</div>

【讨论】:

  • 您能否建议我如何从该列表中删除重复项。如果添加两次,请说functionalDomainName="Supporting Applications",在表中只显示一个值
  • 这是因为 SubDomain 是一个 child..where functionalDomainName 是 parent..
  • 你的桌子怎么样?你能详细说明你需要如何展示它们吗?
  • like functionalDomainName:Supporting Application can have functionaSublDomainName: Document Logistics Activity Monitoring and Business Rules等等..但是现在显示了两个Support Application每个孩子..
  • 我在我的答案检查中添加了一个示例并查看了一次。
猜你喜欢
  • 2019-10-27
  • 2023-01-25
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多