如何从多个 json 数组中获取数据?
2017-05-01
41
我有以下来自 codeigniter 控制器的 json 输出。我想获取
res3
和
res4
的数据,但我无法获取。任何人都可以帮助我,我该怎么做。
<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":"[email protected]",
"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":"[email protected]","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":"[email protected]","A_Paper_Name":"ss-1","A_Paper_Type":"ss","A_Submit_Status":"success","A_Date_Time":"2017-04-21 04:33:11"}]}
我尝试了上面的代码,但它不起作用...
1个回答
res3
&
res4
是单个对象的数组。因此执行
res3[0]
&
rers[0]
将提供其数据
brk
2017-05-01