I am trying to iterate through a stringified json data using angularjs but I am getting undefined on outputing the variables. My json format is shown below on picking from the console
{"data":[
{"id":1,"firstname":"jhfhfh","lastname":"hchch","middlename":null,"dob":"hhc","gender":"hhhhchch","nat":"chhch","phonenumber":null,"idn:"chch",
"email":"[email protected]","username":"cat","password":"password","country":"hshh","state":"hdhdh","city":"hdhdh","address":"area","confirmedEmail":null,"dateOfReg":"hhc"}],
"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"/up","requestHeaders":{"Accept":"application/json"},
"params":{"email":"[email protected]"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"OK"}
here is my angular function
getData: function (email) {
$http({
method: 'GET',
url: '/up',
requestHeaders: {Accept: 'application/json'},
params: {email: email}
}).then(function successCallback(res) {
if (res.status == 204) {
} else if (res.status == 200) {
var myJSON = JSON.stringify(res);
console.log(myJSON); //prints the data
var variable = '';
var name = '';
angular.forEach(myJSON,function(item) { //attempting to do the iteration here
variable += item.email ;
name += item.username ;
})
alert(variable); //outputs undefined
}
}, function errorCallback(response) {
alert(JSON.stringify(response));
});
}
}
how do I loop through the json data
.thenyou needres.datainstead ofres. Also why are you stringifying it?res.data, notres. So for your emails you needres.data.email