2

This is my simplified JSON file content:

{
    "array": {
        "Table1": [
            {
                "h1": 0,
                "h2": 1069
            },
            {
                "h1": 587,
                "h2": 947
            }
        ],
        "Table2": [
            {
                "h1": 13,
                "h2": 0
            },
            {
                "h1": 30,
                "h2": 0
            },
            {
                "h1": 75,
                "h2": 0
            }
        ]
    }
}

This is what my console output after Angular $http call

var data = response.data;
$log.log(data); //Object
$log.log(data.Table1); //Array
$log.log(data.Table1[0]); //Object
$log.log(data.Table1[1].h1); //587

I tried below code but failed to get the value. Please help.

for(var a in data){ //this one correctly loops 2 times
   for(var d in a){ //I have problem with this one
     $log.log(d.h1);
   }
}
4
  • 2
    guess it should be ... for(var d in data[a]){ ... Commented Aug 31, 2016 at 10:12
  • This is not JSON, it is just an Object. Commented Aug 31, 2016 at 10:15
  • change your line to this.. it will work for(var d in data[a]) because you are calling inner object. Commented Aug 31, 2016 at 10:18
  • Wow!! Thanks guys.. I will accept after 7 minutes.. (SO limitation) Commented Aug 31, 2016 at 10:19

4 Answers 4

1

You can do this in below way:-

 $data = file_get_contents('your json data is here.json');
 $content = json_decode($data); 
 foreach($content as $row){
    foreach($row as $data_array){
      foreach($data_array as $item){
       echo 'H1 data item - '.$item->h1;
       echo '    H2 data item - '.$item->h2.'<br>';
        }
      }
   }
Sign up to request clarification or add additional context in comments.

Comments

0

for (var a in data) a would be the key, so Table1 or Table2. The actual value is in data[a]. Also you can not traverse an Array with for (var i in xx) method, as far as I know

for(var a in data){ //this one correctly loops 2 times
   for(var d = 0; d < data[a].length; d++){
     $log.log(data[a][d].h1);
   }
}

Comments

0

It should be using data[a] to get inner object. Basically you would be accessing object by its key

for(var a in data){
   for(var d in data[a]){ 
     $log.log(d.h1);
   }
}

Comments

0

First of all, you have problems with nesting. Your structure is data -> array -> table -> h1. Second, ther for-in construction iterates by the key. So, the a variable in construction for(var i in myArr) refers to the element key, not to the object in array. So, the whole construction in your case may look like this

for(var a in data.array){
   for(var d in data.array[a]){
     console.log(data.array[a][d].h1);
   }
}

P.S. have a look at map() function

1 Comment

Thanks for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.