我试图在我的JSON对象中显示嵌套项目。现在它们的值是[object Object]。我使用$ .each循环,但它似乎没有得到嵌套项的值。我究竟做错了什么?
JSON
{
"AssetGUID":"00000000-0000-0000-0000-000000000000",
"AwayForRepair":false,
"BooleanDataItems":[
{
"Column":null,
"DisplayValue":null,
"TableName":null,
"Value":false
}
],
"ConditionID":0,
"DecimalDataItems":[
{
"Column":null,
"DisplayValue":null,
"TableName":null,
"Value":0
}
],
"DeviceName":null,
"Faulty":false,
"ForDisposal":false,
"ImageDataItems":[
{
"Column":null,
"DisplayValue":null,
"TableName":null,
"Value":null
}
],
"InspectionDate":"\/Date(-62135596800000+0000)\/",
"InspectionPassed":false,
"InspectionType":0,
"IntegerDataItems":[
{
"Column":null,
"DisplayValue":null,
"TableName":null,
"Value":0
}
],
"LocationGUID":"00000000-0000-0000-0000-000000000000",
"StringDataItems":[
{
"Column":null,
"DisplayValue":null,
"TableName":null,
"Value":null
}
],
"TagTypeID":0,
"TransactionGUID":null,
"UserID":0
}
的jQuery
var content = '';
$.each(data, function(i, post) {
content += '<li>' + i + " : " + post + '</li>';
});
$("#addJSON").html(content);
输出
AssetGUID : 00000000-0000-0000-0000-000000000000
AwayForRepair : false
BooleanDataItems : [object Object]
ConditionID : 0
DecimalDataItems : [object Object]
DeviceName : null
Faulty : false
ForDisposal : false
ImageDataItems : [object Object]
InspectionDate : /Date(-62135596800000+0000)/
InspectionPassed : false
InspectionType : 0
IntegerDataItems : [object Object]
LocationGUID : 00000000-0000-0000-0000-000000000000
StringDataItems : [object Object]
TagTypeID : 0
TransactionGUID : null
UserID : 0
答案 0 :(得分:3)
当post
是数组或对象时,您需要递归到它中。
function display_json(data) {
var content = '<ul>';
$.each(data, function(i, post) {
if (post !== null && typeof post == 'object') {
content += display_json(post);
} else {
content += '<li>' + i + " : " + post + '</li>';
}
});
content += '</ul>';
return content;
}