can I use indexOf when looping through parsed Json output to find a particular object if only given a partial amount of it's identifier.
Basically I am calling the Betfair API to get the selectionIds but sometimes I only have the surname of a player and not the full name how is provided by the json response. I think I need something like this but cannot figure it out. The Json data is
var colors = ["blue", "red"];
$("div").click(function() {
$(this).css({ background : colors.reverse()[0] });
});
code is
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 }
This finds the selectionID fine, but if I use only the surname like this I don't get a match
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;
}
}
Can anybody point me in the right direction? Many thanks.
答案 0 :(得分:2)
Your problem is that you are using public class ColorWheel {
// Member variable (properties about the object)
public String[] mColors = {
"#39add1",
"#3079ab",
"#c25975",
"#e15258",
"#f9845b",
"#838cc7",
"#7d669e",
"#53bbb4",
"#e0ab18",
"#637a91",
"#f092b0",
"#b7c0c7",
"#FAEBD7",
"#00FFFF",
"#7FFFD4",
"#0000FF",
"#8A2BE2",
"#A52A2A",
"#DEB887",
"#5F9EA0",
"#7FFF00",
"#D2691E",
"#6495ED",
"#DC143C",
"#B8860B",
"#A9A9A9",
"#006400",
"#FF8C00",
"#8B0000",
"#FFD700",
"#FF69B4",
"#4B0082",
"#F08080",
"#90EE90",
"#87CEFA",
"#FF4500",
"#DA70D6",
"#FA8072",
"#9ACD32",
"#00FF7F",
"#4682B4",
};
// Method (abilities: things the object can do)
public int getColor(){
String color = "";
// Randomly select a fact
Random randomGenerator = new Random(); // Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;
}
}
. However, > 0
will find the surname that you use in the first position, and return indexOf
. Use 0
or > -1
instead.