Currently, I am facing an issue with populating an ng-grid instance using JSON received from a rest API. In the past, I successfully tested this setup and it was working without any configuration problems. The previous JSON response had a top-level "users" object containing an array, making it easy for me to access user data as needed.
However, the API has now been updated, and it is returning each user as an object with a unique incrementing integer as the key. This change has left me unsure about how to access these properties. Below is a simplified version of the new JSON model:
{
"1": {
"id":"1",
"first_name":"Bob",
"last_name":"Jones",
"email":"bob@example.com",
"status":"1"
},
"2": {
"id":"2",
"first_name":"Mike",
"last_name":"Jones",
"email":"mike@example.com",
"status":"1"
},
"3": {
"id":"3",
"first_name":"John",
"last_name":"Jones",
"email":"john@example.com",
"status":"0"
}
}
In my controller, I have configured the grid setup like this:
$scope.usersList = usersFactory.list.query()
.then(function (data){
return data;
}, function (error){
console.log('Oops! Something went wrong.');
});
And then defined the userListGrid as:
$scope.userListGrid = {
data: 'usersList',
enableSorting: true,
columnDefs: [
{field:'first_name', displayName:'First name'},
{field:'last_name', displayName:'Last name'},
{field:'email', displayName:'Email'},
{field:'status', displayName:'Status'}
]};
My main query is regarding how to access the child objects in the new JSON response without knowing their exact keys. Do I need to iterate over all of them and organize them into an array first?
Any assistance on this matter would be greatly appreciated!