0
如果僅通過解析的Json輸出循環以查找特定對象(如果僅給出其部分數量的標識符),我可以使用indexOf。當使用Javascript訪問JSON時使用indexOf
基本上我打電話給Betfair API來獲取選擇標識,但有時候我只有一個球員的姓氏而不是全名,json響應是如何提供的。我想我需要這樣的東西,但無法弄清楚。 JSON數據是
var response = { jsonrpc: '2.0',
result:
[ { marketId: '1.118739296',
marketName: 'Match Odds',
marketStartTime: '2015-05-13T01:00:00.000Z',
totalMatched: 17,
runners:
[ { selectionId: 7750118,
runnerName: 'Makoto Ninomiya',
handicap: 0,
sortPriority: 1,
metadata: { runnerId: '7750118' } },
{ selectionId: 7659425,
runnerName: 'Su Jeong Jang',
handicap: 0,
sortPriority: 2,
metadata: { runnerId: '7659425' } } ],
eventType: { id: '2', name: 'Tennis' },
competition: { id: '7354189', name: 'ITF Women Kurume 2015' },
event:
{ id: '27442711',
name: 'Ninomiya v Jang',
countryCode: 'JP',
timezone: 'Japan',
openDate: '2015-05-13T01:00:00.000Z' } } ],
id: 1 }
代碼
for (var i = 0; i<= Object.keys(response.result).length; i++) {
if(response.result[0].runners[i].runnerName == 'Su Jeong Jang'){
document.body.innerHTML = response.result[0].runners[i].selectionId;
}
}
此發現selectionID罰款,但如果我只用姓氏這樣我沒有得到一個匹配
for (var i = 0; i<= Object.keys(response.result).length; i++) {
if(response.result[0].runners[i].runnerName.indexOf('Su Jeong') >0 {
document.body.innerHTML = response.result[0].runners[i].selectionId;
}
}
燦有人指出我正確的方向? 非常感謝。
'我<= Object.keys(response.result).length'看起來不正確。不應該是'我
Bergi
https://jsfiddle.net/rootsupcycle/df4zfp8o/3/ –
你將不得不使用一些有效的'i':https://jsfiddle.net/df4zfp8o/4/ – Bergi