1

I'm trying to display nested items in my JSON object. Right now their value is [object Object]. I'm using a $.each loop but it doesn't seem to be getting the values of the nested items. What am I doing wrong?

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);

Output

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
2
  • This might be helpful to you. stackoverflow.com/questions/8553539/… Commented Jun 4, 2014 at 20:45
  • each() is doing what it's supposed to do. It iterates over each key/value pair in your data object - it does not then burrow down into any values that are themselves objects. See, for example, this question for more info. Commented Jun 4, 2014 at 20:45

1 Answer 1

3

When post is an array or object, you need to recurse into it.

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

hmm, the value of the nested objects is now undefined.
Forgot the return content statement. It works now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.