我正在使用微型服务进行足球比赛中的比赛。每个MATCH都有一个HOME团队和AWAY团队。
matches" : [ {
"start_at" : "2018-06-14T18:00:00.000+0000",
"num" : 1,
"knockout" : false,
"scoreHome" : 0,
"scoreAway" : 0,
"scoreHomeET" : 0,
"scoreAwayET" : 0,
"scoreHomePenalties" : 0,
"scoreAwayPenalties" : 0,
"resultRegularTime" : 0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/matches/1"
},
"match" : {
"href" : "http://localhost:8080/matches/1"
},
"away" : {
"href" : "http://localhost:8080/matches/1/away"
},
"home" : {
"href" : "http://localhost:8080/matches/1/home"
},
"round" : {
"href" : "http://localhost:8080/matches/1/round"
}
}
},
在我的反应应用程序中,我正在用axios读取这些值。一切都很好,但获取链接的JSON数据的更好方法是什么?
我想在链接中获得客队的价值:“离开”:{“href”:“http://localhost:8080/matches/1/away”},
像const awayTeam = {match.away}之类的东西
有什么更好的方法呢?
谢谢
这是你如何使用URLSearchParams
做到这一点:
var paramsString = "link=away&element=0";
var searchParams = new URLSearchParams(paramsString);
var data= [{
"start_at" : "2018-06-14T18:00:00.000+0000",
"num" : 1,
"knockout" : false,
"scoreHome" : 0,
"scoreAway" : 0,
"scoreHomeET" : 0,
"scoreAwayET" : 0,
"scoreHomePenalties" : 0,
"scoreAwayPenalties" : 0,
"resultRegularTime" : 0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/matches/1"
},
"match" : {
"href" : "http://localhost:8080/matches/1"
},
"away" : {
"href" : "http://localhost:8080/matches/1/away"
},
"home" : {
"href" : "http://localhost:8080/matches/1/home"
},
"round" : {
"href" : "http://localhost:8080/matches/1/round"
}
}
}];
var numberofElement = parseInt(searchParams.get("element"));
var kindofLink = searchParams.get("link");
console.log(data[[numberofElement]]._links[kindofLink].href);
这是一个mozilla.org with example on how to use URLSearchParams similar to mine的链接。