我有一个匹配的JSON数组:
{
"matches": [{
"match": {
"player1_id": 35161770,
"player2_id": 35161786,
"winner_id": 35161770,
"loser_id": 35161786,
}
}, {
"match": {
"player1_id": 35161812,
"player2_id": 35161778,
"winner_id": 35161812,
"loser_id": 35161778,
}
}, {
"match": {
"player1_id": 35161770,
"player2_id": 35161775,
"winner_id": 35161775,
"loser_id": 35161770,
}
}]
}
我正在迭代这个数组,如下所示:
for (int i = 0; i < matches.length(); i++)
{
JSONObject object = matches.getJSONObject(i);
JSONObject match = object.getJSONObject("match");
if (match.get("winner_id") == match.get("player1_id"))
{
Log.d("result", "one");
}
else if (match.get("winner_id") == match.get("player2_id"))
{
Log.d("result", "two");
}
else
{
Log.d("result", "tie");
}
}
但是,无论我做什么,结果总是显示为tie
...
我在这里做错了什么?
答案 0 :(得分:2)
JSONObject.get("")
将返回Object
。您可以将其更改为:
match.getString("winner_id").equals(match.getString("player1_id"))
正如@gj_指出的那样,你也可以使用以下内容:
match.getInt("winner_id") == match.getInd("player1_id")
答案 1 :(得分:0)
尝试使用:
JSONObject object = matches.optJSONObject(i);