0

I had a JSON response as below

{"items":["1","2","3"],"code":"ok"}

var simpleArry = [] ;
$http.post(url, "", "")
            .then(function (response) {
                console.log(response.data.items);
                //$scope.items= response.data.items;
                //instead of the above line 
                simpleArry = resp.data.items;
            });

I need the array to iterate again to get some data from another call . need it in a list/array which it should look like

var itmsArry = ["1","2","3"]

I tried something like this

var simpleArry = [] ;
simpleArry = resp.data.items;

but it always empty . What went wrong with it

My Question was not duplicate . Its about the json array and nothing to do with async call

User146378
  • 215
  • 1
  • 3
  • 11

4 Answers4

0

It should work with the code you showed, Probably you are assigning outside the response,

app.controller('myController', function($scope, myService) {
  myService.getJSON().then(function(data){
            $scope.myData =data;
            console.log(data);
 });

});
app.service('myService', function($http) {
  this.getJSON = function() {
    return $http.get('test.json').then(function(data) {
      return data.data.items;
    });
  };
});

Demo

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

you should try this below the code

javascript code

var str='{"items":["1","2","3"],"code":"ok"}';
var data=JSON.parse(str);
console.log(data.items);

output enter image description here

Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
0

Use

try{
    var data = JSON.parse(response)
}catch{

}    

data.item contains the array.

Jibin Mathew
  • 4,816
  • 4
  • 40
  • 68
0

This is how i solved .

var testArry = [];

for (var r = 0; r < response.data.items.length ; r++){
  testArry.push(response.data.items[r]);                  
}
User146378
  • 215
  • 1
  • 3
  • 11