问题
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