gpt4 book ai didi

javascript - 如何检查 JSON 对象 ionic2 中的值

转载 作者:行者123 更新时间:2023-12-01 04:06:18 24 4
gpt4 key购买 nike

如何查看"no_cover"thumbnail[0]并将其替换为 asset/sss.jpg用于展示listpage我尝试设置<img src="{{item.LINKS.thumbnail[0]}}">在Listpage.html中,仅显示缩略图[0]为http://xxx.jpg , http://xxx.jpg , ,但是我不知道要设置“no_cover”

myjson

"    DOCSET":{
"DOC":[
{
"LINKS":{

"thumbnail":"http://yyy.jpg",
"thumbnail":"http://....jpg"}
}],
"DOC":[
{
"LINKS":{

"thumbnail":"http://xxx.jpg",
"thumbnail":"http://....jpg"}
}],
"DOC":[
{
"LINKS":{

"thumbnail":"no_cover", <<<<
"thumbnail":"http://....jpg"}
}]
}
<小时/>

编辑 - 您的 JSON 应该是什么样子

因为我 (ivaro18) 相信您是手工编写 JSON 并且没有复制响应对象。让我向您展示有效的 JSON 是什么样子的:

{
"DOCSET" : [
{
"DOC" : {
"LINKS" : [
{
"thumbnail" : "http://.....jpg"
},
{
"thumbnail" : "http://.....jpg"
}
]
}
},
{
"DOC" : {
"LINKS" : [
{
"thumbnail" : "http://.....jpg"
},
{
"thumbnail" : "http://.....jpg"
}
]
}
},
{
"DOC" : {
"LINKS" : [
{
"thumbnail" : "no_cover"
},
{
"thumbnail" : "http://.....jpg"
}
]
}
}
]
}

编辑结束

<小时/>

listpage.html

<ion-list>
<ion-item *ngFor="let item of foundRepos">
<ion-thumbnail item-left >
<img src="{{item.LINKS.thumbnail[0]}}"> <!--I have no idea to set it -->
</ion-thumbnail>
</ion-list>

列表.page.ts

this.http.get("my_url")
.subscribe(data =>{
this.foundRepos = data.json().DOCSET.DOC;
},error=>{
err => console.error(err),
() => console.log('getRepos completed')
);

最佳答案

所以,首先您发布的 JSON 无效。用它可能是什么编辑了你的问题(因为你正在按照你所说的打印它)

我从您的问题中了解到以下内容,您有 <img src="{{item.LINKS.thumbnail[0]}}">但这会引发 404no_cover返回,这就是为什么,当item.LINKS.thumbnail[0]时等于'no_cover'您想要显示图像assets/sss.jpg

那么,我们如何使用它来显示 'assets/sss.jpg'而不是'no_cover' ?它被称为 Elvis operator ( ? : )

让我们假设 if 具有以下结构: -声明:

if(expression) {
result = whenExpressionIsTrue
} else {
result = whenExpressionIsFalse
}

我们可以用 Elvis 运算符重写它,如下所示:

result = someExpression ? whenExpressionIsTrue : whenExpressionIsFalse

那么,让我们检查一下您的问题,如何使用 if 来做到这一点-声明?

if(item.LINKS.thumbnail[0] == 'no_cover') {
someVariable = 'assets/sss.jpg';
} else {
someVariable == item.LINKS.thumbnail[0];
}

现在用 Elvis 运算符缩短它:

someVariable = item.LINKS.thumbnail[0] == 'no_cover' ? 'assets/sss.jpg' : item.LINKS.thumbnail[0];

现在可以在 HTML 中使用它,如下所示:

<img 
[src]="item.LINKS.thumbnails[0] == 'no_cover' ? 'assets/sss.jpg' : item.LINKS.thumbnails[0]"
/>

关于javascript - 如何检查 JSON 对象 ionic2 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41804719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com