Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This is a question I truly feel stupid for asking. But I have searched the web and haven't found a specific example with my problem. Luckily, the question is simple. I have the following JSON data being read in by AngularJS as follows:

return $http.post('getData.htm').then(function(response) {
    console.log("response.data: " + response.data);
    var roles = angular.fromJson(response.data).model.results;

    return roles;
  });

The console in this scenario outputs the following:

 response.data: {"model":{"results":["1","2","3","4","5","6","7","8","9","10","11"],"totalCount":11}}

I expect roles to contain an array with those numbers, however when I print out the value of roles it simply says [object Object].

How do I access the numbers contained in the results array?

thanks a lot!

share|improve this question
    
Did you try to print as: JSON.stringify(response.data)? – Maxim Shoustin Jul 3 '14 at 11:36
1  
response.data is all you need. In adding the angular.fromJson call, you're effectively creating another object, deserialized from the JSON response. – Ben Jul 3 '14 at 11:47
up vote 2 down vote accepted

A javascript Array is also an Object so maybe when you did var roles = angular.fromJson(response.data).model.results; you got the array you wanted. Try roles[1]

share|improve this answer
    
This was indeed the answer. I swear I tried this a billion times. That's programming, I guess :). thank you for the help! – insomniac Jul 3 '14 at 12:17
    
Thanks for that answer!! I searched the web and found everyone using JSON.parse but unfortunately that didn't worked. Your answer worked perfectly. – nkt24 Dec 5 '15 at 11:20

Don't try to "load from JSON" a JSON object.

response.data is already transformed in JSON format by angular.. Just use it like that :

console.log(response.data.model);
console.log(response.data.model.results);
console.log(response.data.model.results[1]);
share|improve this answer
    
When I try this, the following occurs: The first line outputs undefined and the second causes TypeError: Cannot read property 'results' of undefined – insomniac Jul 3 '14 at 11:56
    
if console.log(response.data) returns you an object with a 'model' key, it should definitely work... – Cétia Jul 3 '14 at 11:59

http://jsbin.com/duruy/1/edit

app.controller('firstCtrl', function($scope, $http){

activate();
  $scope.roles = [];

function activate ()
  {

    return $http.get('http://jsbin.com/yewefo/1').then(function(response) {
    console.log(response.data.model);
   angular.copy(response.data.model.results, $scope.roles);


  });

  }

});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.