How to convert JSON Strings to arrays in Javascript ionic?

浪尽此生 提交于 2020-06-01 07:40:30

问题


Suppose i have this json string data returned from an API URL which is already in JSON: [{ "images":"[\"A.jpg\",\"B.jpg\",\"C.jpg\"]" }]

In my html page when i wanted to call the image, i wrote it like this:

<ion-card-header *ngFor="let data of datauser">
<img src="https://example.com/images/{{data.images}}">
</ion-card-header>

But it displays as an empty image with this src in the console:

"https://example.com/images/["A.jpg","B","C.jpg"]"

& when i access the first image like this:

<ion-card-header *ngFor="let data of datauser">
    <img src="https://example.com/images/{{data.images[0]}}">
    </ion-card-header> 

it also displays as an empty image with this src in the console: src="https://example/images/["

So what's the solution please, i want the src to be like this src="https://example/images/A.jpg"

In case u want to see my .ts code:

@Component({
  selector: 'app-landing-page',
  templateUrl: './landing-page.page.html',
  styleUrls: ['./landing-page.page.scss'],
})
export class LandingPagePage implements OnInit {
  datauser: String;
  constructor( public api: ApiLandingRecipesService) { 

  }
  ngOnInit() {
    this.getDataUser();
  }
  async getDataUser() {
    await this.api.getDataUser()
      .subscribe(res => {
        console.log(res);
        this.datauser = res;
        console.log(this.datauser);
      }, err => {
        console.log(err);
      });
  }



}  

来源:https://stackoverflow.com/questions/62114855/how-to-convert-json-strings-to-arrays-in-javascript-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!