jQuery在JSON值上返回undefined

时间:2016-06-14 13:35:07

标签: jquery json

我在下面有以下AJAX代码,它从data.json

获取值

我正在使用jQuery来获取值,但是当我尝试获取其中一些值时,我得到了未定义 "输入":" BIndex",             " type2":"等级",             " rating1":" A +",             " shortDesc":"这是行业平均水平。",             " toolTipText":" B lorem ipsum 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."
        }]
    }]
}

2 个答案:

答案 0 :(得分: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);
             })

答案 1 :(得分:0)

您是否尝试将其从JSON解析为对象?

var obj = JSON.parse(text);

我的小提琴:

https://jsfiddle.net/rphqt9y5/2/