0

this is the JSON document I'm trying to read data from:

{title": "some name here",
                "details": {
                  "color": "red",
                  "location": "at home",
                  "shape": "square"
                }}

I'm trying to read this data in my js file and then add this to html. Here's my code:

$http.get('/api/dataIuse')
  .success(function(rdata) {
    $scope.title = rdata['title'];
    $scope.details = rdata['details'];
    $scope.fullData = rdata;
    console.log(rdata['title']);
  })
  .error(function(rdata) {
    console.log('Error: ' + rdata);
  });

How can I read inside of 'details' to get color, location and shape seperately??

This is my guess (it doesn't work):

$scope.details.color = rdata['details.color'];
$scope.details.location = rdata['details.location'];
$scope.details.shape = rdata['details.shape'];

What method of reading JSON should I use?

Thanks!

1 Answers1

0

Try the following:

var rdata = {"title": "some name here",
                "details": {
                  "color": "red",
                  "location": "at home",
                  "shape": "square"
                }}
                
var color = rdata.details.color;
var area = rdata.details.location;
var shape = rdata.details.shape;
console.log(color);
console.log(area);
console.log(shape);
//or if you want to iterate through the object
Object.keys(rdata.details).forEach(function(item){
  console.log(rdata.details[item]);
})
Mamun
  • 66,969
  • 9
  • 47
  • 59