如何从多个json数组中获取数据。

时间:2017-05-01 08:15:35

标签: javascript jquery json ajax

我在codeigniter控制器的json中有以下输出。我想获取数据 res3res4,但我无法得到它。任何人都可以帮助我如何做到这一点。

<script>
function createDiv(data) {
  var dynamicHTML = '';
  var innerContent = '';
  alert(data.res4.Status);
  for (var i = 0; i < data.res2.length; i++) {


            if(data.res2[i].paid_type == 1) {

                    if(data.res4.A_Submit_Status == "success") {
                      innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                      '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +
                      '<span class="input-group-btn" style="padding-top:10px;">' +
                        '<button class="btn btn-info" type="button">Analysis 1</button>' +
                      '</span>'+
                      '</div>';
                    } else {
                        innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                        '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +        
                        '<span class="input-group-btn" style="padding-top:10px;">' +
                            '<button class="btn btn-info" type="button">Take Test 1</button>' +
                        '</span>'+
                        '</div>';
                    }

            } else {
                    if(data.res4.A_Submit_Status == "success"){
                        innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                        '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +                                
                        '<span class="input-group-btn" style="padding-top:10px;">' +
                            '<button class="btn btn-info" type="button">Analysis 2</button>' +
                        '</span>'+
                        '</div>'; 
                    } else {
                        innerContent +=  '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                        '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +
                        '<span class="input-group-btn" style="padding-top:10px;">' +
                            '<button class="btn btn-info" type="button">Take Test</button>' +
                        '</span>'+
                        '</div>';
                    }
             }
        }

  dynamicHTML += '<div id="' + data.res2.Paper_Type + '" class="tabcontent" style="border:none;">' + innerContent + '</div>';
  $('div#tabs_data').append(dynamicHTML);
}
</script>

输出就在这里。

{"user":"abc@gmail.com",
    "res2":[{"id":"1","Paper_Name":"sm1","Paper_Type":"ss","paid_type":"0"},{"id":"2","Paper_Name":"    ss-2","Paper_Type":"ss","paid_type":"1"},{"id":"3","Paper_Name":"ss-3","Paper_Type":"ss","paid_type":"1"},{"id":"4","Paper_Name":"ss-4","Paper_Type":"ss","paid_type":"1"}],
    "res3":[{"User_id":"abc@gmail.com","Name":"mahi mahi","Phone":"2147483647","Txnid":"2147483647","Amount":"120","Product_Info":"ss","encryptedPaymentId":"CBEC29993C2DAE0B37453BFAD285CB87","Status":"success","Date_Time":"2017-04-20 12:22:49"}],
    "res4":[{"A_User_id":"abc@gmail.com","A_Paper_Name":"ss-1","A_Paper_Type":"ss","A_Submit_Status":"success","A_Date_Time":"2017-04-21 04:33:11"}]}

我尝试上面的代码,但它不起作用......

1 个答案:

答案 0 :(得分:1)

res3&amp; res4是单个对象的数组。这样做

res3[0]&amp; rers[0]会提供他们的数据