大家好我是ajax面对一个问题我希望将这些数据显示为html中的表格如何提取这些数据并以html表格形式显示它只是一个虚拟数据,否则它有很多对象加上嵌套对象请帮帮我。
JSON DATA:
{
"FullName":"shahrushabh",
"description":"this is demo person register",
"Address1":"b\/1",
"Area":"Sabarmati",
"status":"active",
"Thumb":"0",
"checkbox":
[
{"ID":"1","UniqueID":"617993","Product":"electronics","Image":"617993\/alphansomangolips.jpg"},
{"ID":"2","UniqueID":"617993","Product":"Home Decor","Image":"617993\/banana.jpg"}
]
}
AJAX代码:
<script>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url()?>index.php/myad",
type: 'GET',
dataType:'json',
success: function(data){
var jsonobject = jQuery.parseJSON(data);
alert("hi");
$.each(jsonobject, function(key, value){
console.log(value.FullName);
$.each(value.checkbox , function(k, val){
console.log(val);
})
})
}
});
});
</script>
答案 0 :(得分:2)
我假设您的问题是您有嵌套对象,下面的代码是将每个用于嵌套对象。
//Main loop
$.each(jsonObject, function(key, value){
console.log(value.FullName);
//Inner loop for check box for each user
$.each(value.checkbox, function(k, val){
console.log(val);
})
});
请确保在使用之前阅读Official each()。
修改强>
在迭代之前检查响应是否为数组。
if (!$.isArray(jsonObject)) {
jsonObject = [jsonObject];
}
$.each(jsonObject, function(key, value){
console.log("FULL NAME " + value.FullName);
$.each(value.checkbox, function(k, val){
console.log("ID" + " " + val.ID);
});
});
var jsonObject = [
{
"FullName":"shahrushabh",
"description":"this is demo person register",
"Address1":"b\/1",
"Area":"Sabarmati",
"status":"active",
"Thumb":"0",
"checkbox":
[
{"ID":"1","UniqueID":"617993","Product":"electronics","Image":"617993\/alphansomangolips.jpg"},
{"ID":"2","UniqueID":"617993","Product":"Home Decor","Image":"617993\/banana.jpg"}
]
},
{
"FullName":"shahrushabh",
"description":"this is demo person register",
"Address1":"b\/1",
"Area":"Sabarmati",
"status":"active",
"Thumb":"0",
"checkbox":
[
{"ID":"1","UniqueID":"617993","Product":"electronics","Image":"617993\/alphansomangolips.jpg"},
{"ID":"2","UniqueID":"617993","Product":"Home Decor","Image":"617993\/banana.jpg"}
]
}
];
//console.log(jsonObject);
$.each(jsonObject, function(key, value){
console.log(value.FullName);
$.each(value.checkbox, function(k, val){
console.log(val);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>