2016-06-14 90 views
-1

我有以下的AJAX代码下面从data.json获取值对JSON jQuery的返回undefined值

我使用jQuery来获取值,但我得到不确定的,当我试图让一些值
“类型”: “BIndex”, “TYPE2”: “级”, “额定值1”: “A +”, “shortDesc”: “这是行业平均水平。”, “的ToolTipText”:“B Lorem存有sososms“。

我试图将它们作为 nestedValue.type和nestedValue.type2

我怎样才能获得这些价值?

这是我的代码和JSON下面

的JavaScript

<script type="text/JavaScript"> 

$(document).ready(function() { 

    var jsonData ="data.json"; 
    console.log("jsonData " +jsonData); 
    var ajaxURL = jsonData; 

    $.ajax({ 
     url: ajaxURL, 
     dataType: "json", 
     //jsonp:"jsoncallback", 
     success: function(data, textStatus, jqXHR) { 
     // console.log("Data " + data); 
    // console.log("bankName " + data.bankName); 
    // console.log("type " + data.type); 

     console.log("data, textStatus, jqXHR " + data + textStatus + jqXHR) 
      jQuery.each(data, function(key,value) { 

       console.log("type " + key + value); 

      }); 


      jQuery.each(data.data[0].stressType, function(nestedKey,nestedValue) { 

        console.log(" aaa " + nestedValue[i].type); 
      console.log(" aaa " + nestedValue[i].type2); 
       });  


     var jsonVals = ""; 
     jsonVals += '<div class="row">'; 


     jsonVals +="<div class='col-md-2 col-sm-2 col-xs-6'>"; 
     jsonVals +='<div class="name" >' + data.bankName+'</div>'; 
     jsonVals +='<div class="account">' + data.holdingCompany +'</div>'; 
     jsonVals +='<div class="approved">' + data.streetAddress+'</div>'; 

     jsonVals +="</div>"; 

     $("#clientData").append(jsonVals); 
    //});   

    }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log('FAILED to get JSON from AJAX call' + jqXHR + textStatus + errorThrown); 

     } 
    });  



}); 

    </script> 

data.json

{ 
       "bankName": " Community of Springfield", 
       "holdingCompany": " Holdings LLC", 
       "streetAddress": "123 Main St", 
       "city": "Your Town Name", 
       "stateZip": "CA-94012", 
       "ceritificate": "Certificate #139", 
       "Rating": "Ratings ",  
    "cTitle": "Certificate #19", 
       "cData": [{ 
           "clRating": "C", 
           "cName": "Cal Ad", 
           "cScore": "1" 

       }, 
       { 
           "clRating": "A", 
           "cName": "Cal Aq", 
           "cScore": "1" 
       }, 
           { 
           "clRating": "M", 
           "cName": "Cal Aq", 
           "cScore": "1" 
       }, 
           { 
           "clRating": "E", 
           "cName": "Cal Ay", 
           "cScore": "1" 
       }, 
           { 
           "clRating": "L", 
           "cScore": "1", 
           "cName": "Cal Ad" 
       }, 
           { 
           "clRating": "S", 
           "cName": "CalAcy", 
           "cScore": "1" 
       }], 

       "data":[{ 
     "stressType": [{ 
      "type": "BIndex", 
      "type2": "Grade", 
      "rating1": "A+", 
      "shortDesc": "This is industry average.", 
      "toolTipText": "B lorem ipsum sososms." 
     }, 
     { 
      "type": " Counterparty Score", 
      "type2": "Score", 
      "rating1": "8", 
      "shortDesc": "This institution exhibits strong lorem ipsum lorem ipsum lorem ipsum .", 
      "toolTipText": "TI is a lldslsa bank." 
     }, 
     { 
      "type": "Regulatory y", 
      "type2": "Condition", 
      "rating1": "Adequate", 
      "shortDesc": "This institution's ability to absorb losses alorem eipsum loelsmy.", 
      "toolTipText": "Btoltip slorem ipsum." 
     }, 
     { 
      "type": "Shadow Zone", 
      "type2": "Score1", 
      "rating1": "323.18", 
      "type2": "Score2", 
      "rating2": "32.25", 
      "shortDesc": " stress level .", 
      "toolTipText": " leading indicators." 
     }] 
    }] 
} 
+1

你是什么意思:'但我没有找回所有的values'?如果你没有找回想要的结果,那么你需要检查服务器端脚本。 – vaso123

+0

这不是服务器这是所有cleint端,我试图得到这些值,但我得到undefined“type”:“BIndex”, “type2”:“Grade”, “rating1”:“A +”, “ shortDesc“:”这是行业平均水平“, ”toolTipText“:”B lorem ipsum sososms“。 – user244394

+0

看看我的固定小提琴链接 – DannyThunder

回答

0

使用data.data [0] .stressType循环。

jQuery.each(data.data[0].stressType, function(i,nestedValue) { 

       console.log(" aaa " + nestedValue[i].type); 
       console.log(" aaa " + nestedValue[i].type2); 
      }) 
+0

我试过你的建议,但我得到这个未捕获的Typerror:不能读取未定义的属性类型。是什么原因造成的? – user244394