midhunsivarajan
midhunsivarajan

Reputation: 49

How to get json data to an angular array variable

I am trying to get a json result to an array object .I am echoing a json data in php. like this.

    <?php
header('Access-Control-Allow-Origin: *');
header('Content-type:application/json;charset=utf-8');
$arr=  '[{
   "id": "1",
   "date": "2020-03-21",
   "status": "present",
   "studentid": "1"
  },
  {
   "id": "2",
   "date": "2020-03-24",
   "status": "present",
   "studentid": "1"
  },
  {
   "id": "3",
   "date": "2020-03-25",
   "status": "absent",
   "studentid": "1"
  },
  {
   "id": "4",
   "date": "2020-03-26",
   "status": "absent",
   "studentid": "1"
  }
 ]';

echo $arr;
?>

~


How to get absentees using angular in an array.

Angular part i tried didnt work

      this.http.post("http://localhost/android/Api.php?apicall=getattendance", JSON.stringify(this.postData),options)


          .subscribe( (data) => {
          this.setUsersArray(data);
          console.log(data + "URL DATA"+JSON.stringify(this.postData));

           }


      );

=====================================================================
    setUsersArray(data){

       if (data instanceof Array) {
                       {

                        this.date_present = data.map(function (ele) {
                        if(ele.status==='present')
                        {

                          return ele.date;

                        }



                        });
                        this.date_absent = data.map(function (ele) {
                          if(ele.status==='absent')


                          return ele.date;

                          });
                   }
                    }

I am getting date_absent and date_present as null.Why am i getting this as null.Please help me. I am new to angular.

Upvotes: 0

Views: 149

Answers (2)

CHanaka
CHanaka

Reputation: 502

try for loop in typescript

setUsersArray(data:any){
for(let item of data){
  console.log(item);
  if(item.status==='present') {
    this.present.push(item.date);
  }
  if(item.status==='absent') {
    this.absent.push(item.date);
  }       
}

Upvotes: 1

devesh
devesh

Reputation: 1

Trying to this url $characters = json_decode($data, true); // decode the JSON feed and make an associative array https://www.taniarascia.com/how-to-use-json-data-with-php-or-javascript/

Upvotes: 0

Related Questions