1

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

6
  • Your response is already in json.you dont need to stringfy it. Commented May 17, 2018 at 11:24
  • with .then you need res.data instead of res. Also why are you stringifying it? Commented May 17, 2018 at 11:24
  • how do I loop over it then with the {"data":[ Commented May 17, 2018 at 11:25
  • I am stringifying because res.email keeps outputing undefined. so I decided to stringify it Commented May 17, 2018 at 11:27
  • once, again, you need res.data, not res. So for your emails you need res.data.email Commented May 17, 2018 at 11:28

2 Answers 2

1

You are doing it wrong:

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 = res.data; // <-- THIS IS WHAT I HAVE CHANGED

            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));
    });
}
}

Do var myJSON = res.data; and it'll work. Your res.data is an array, so iterate over it.

Sign up to request clarification or add additional context in comments.

Comments

0

Use JSON.parse for parse string to JSON object, And after if you have response.data as array you can use native forEach methods

1 Comment

Please provide an example for that.

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.